C# delegate
一、委托
當我們需要把方法做為參數傳遞給其他方法的時候,就需要使用委托。
因為有時候,我們要操作的對象,不是針對數據進行的,而是針對某個方法進行的操作。
?????? 我們還是來以代碼入手
??
Codeusing?System;
namespace?gosoa.com.cn
{
????public?class?test
????{
????????public?delegate?string?GetAString();
????????public?static?void?Main()
????????{
????????????int?x=10;
????????????GetAString?firstString=new?GetAString(x.ToString);
????????????Console.WriteLine(firstString());
????????????//上句和下面這句類似。
????????????//Console.WriteLine(x.ToString());?
????????}
????}
}
?
??? 在上例中,public delegate string GetAString(); 就是聲明了一個委托(delegate),其語法和方法的定義類似,只是沒有方法體,前面要加上關鍵字 delegate 。定義一個委托,基本上是定義一個新類,所以,可以在任何定義類的地方,定義委托。
注意,在C#中,委托總是自帶一個有參數的構造函數,這就是為什么在上例中,GetAString firstString=new GetAString(x.ToString); 通過這句初始化一個新的delegate的時候,給傳遞了一個x.ToString 方法。但,在定義delegate的時候,卻沒有定義參數。
??????
在看另一個例子之前,我們先來了解下匿名方法。
?????? 匿名方法的使用,我們看個例子
?
Codeusing?System;
namespace?gosoa.com.cn
{
????public?class?test
????{
????????delegate?string?GetUrl(string?val);
????????static?void?Main(string?[]?args)
????????{
????????????string?domin="www.gosoa.com.cn";
????????????GetUrl?url=delegate(string??param)
????????????{
?????????????????param="http://"+param;
?????????????????return?param;
????????????};
????????????Console.WriteLine(url(domin));?
????????}
????}
}
?
在本例中,GetUrl url=delegate(string? param) 在這里實例化一個delegate的時候,采用了匿名的方法。本例輸出的結果是 http://www.gosoa.com.cn
?
接下來我們再看一個委托的例子。?
?
Codeusing?System;
namespace?gosoa.com.cn
{
????class?NumberOpthion
????{
????????public?static?double?numOne(double?x)
????????{
????????????return?x*2;
????????}
????????public?static?double?numTwo(double?x)
????????{
????????????return?x*x;
????????}
????}
????public?class?delegateTest
????{
????????
????????delegate?double?DoubleOpration(double?x);
????????static?void?printNumber(DoubleOpration?dp,double?x)
????????{
????????????double?result=dp(x);
????????????Console.WriteLine(
????????????????"value?is?{0},?result?of?DoubleOpration?is?{1}:",x,result????
????????????);
????????}
????????static?void?Main()
????????{
?????????????DoubleOpration?doption?=new?DoubleOpration(NumberOpthion.numOne);????
?????????????printNumber(doption,1.5);
?????????????doption?=new?DoubleOpration(NumberOpthion.?numTwo);????
?????????????printNumber(doption,3.2);
????????}
????}
}
?
首先我們定義了一個NumberOpthion類。用來對數字進行*2和2次方運算。接著,我們定義了一個委托delegate double DoubleOpration(double x)。下面,我們定義了printNumber(DoubleOpration dp,double x) 這樣一個方法,其中一個參數就是委托。最后我們DoubleOpration doption =new DoubleOpration(NumberOpthion.numOne);實例化了一個委托,并調用了 printNumber 方法。最后的輸出結果是
Value is 0.5? result of DoubleOpration is 3;
Value is 3.2? result of DoubleOpration is 10.24;
?
在上例中,我們如果采用匿名方法,代碼就會如下:?
?
Codeusing?System;
namespace?gosoa.com.cn
{
????public?class?delegateTest
????{
????????delegate?double?DoubleOpration(double?x);
????????static?void?printNumber(DoubleOpration?dp,double?x)
????????{
????????????double?result=dp(x);
????????????Console.WriteLine(
????????????????"value?is?{0},?result?of?DoubleOpration?is?{1}:",x,result????
????????????);
????????}
????????static?void?Main()
????????{
?????????????DoubleOpration?doptionOne?=delegate(double?x){return?x*2;};
?????????????DoubleOpration?doptionTwo?=delegate(double?x){return?x*x;};
?????????????printNumber(doptionOne,1.5);
?????????????printNumber(doptionTwo,3.2);
????????}
????}
}
?
委托,還有一種情況,是多播委托。這個在以后我們應用到的時候,會學習到。
轉載于:https://www.cnblogs.com/Alexander_2009/archive/2009/10/24/1588940.html
總結
以上是生活随笔為你收集整理的C# delegate的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用的 css hack实例
- 下一篇: ext 从头开始 extjs 控件 触发