委托
一.委托
委托可以理解為一種數據類型(delegate),后面根函數定義.
delegate void Del(int x);
委托概述
委托具有以下特點:
-
委托類似于 C++ 函數指針,但它是類型安全的。
-
委托允許將方法作為參數進行傳遞。
-
委托可用于定義回調方法。
-
委托可以鏈接在一起;例如,可以對一個事件調用多個方法。
-
方法不需要與委托簽名精確匹配。有關更多信息,請參見協變和逆變。
-
C# 2.0 版引入了匿名方法的概念,此類方法允許將代碼塊作為參數傳遞,以代替單獨定義的方法。
二.委托使用.
1.定義一個簽名
2.定義委托變量,并為委托變量賦值(可以是靜態方法/實例.方法).
3.調用.
delegate void Del(int i, double j); //1,定義一個簽名
class MathClass
{static void Main(){MathClass m = new MathClass();
// Delegate instantiation using "MultiplyNumbers"Del d = m.MultiplyNumbers; //2定義一個委托變量,為委托變量賦值
// Invoke the delegate object.System.Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
for (int i = 1; i <= 5; i++)
{d(i, 2); //3.調用}}
// Declare the associated method.void MultiplyNumbers(int m, double n)
{System.Console.Write(m * n + " ");
}
}
三.匿名方法 委托.
要將代碼塊傳遞為委托參數,創建匿名方法則是唯一的方法
將代碼塊賦值給一個匿名變量.
// Create a delegate instancedelegate void Del(int x);
// 將代碼塊賦值給一個委托Del d = delegate(int k) { /*語句1...語句2 do some ... */ };
四.何時使用委托而不使用接口(C# 編程指南)
委托和接口都允許分離類型聲明和實現。給定的接口可由任何類實現;可以為任何類中的方法創建委托,前提是該方法符合委托的方法簽名。接口引用或委托都可由不了解實現該接口或委托方法的類的對象使用。既然存在這些相似性,那么何時應使用委托,何時又該使用接口呢?
在以下情況中使用委托:
-
當使用事件設計模式時。
-
當封裝靜態方法可取時。
-
當調用方不需要訪問實現該方法的對象中的其他屬性、方法或接口時。
-
需要方便的組合。
-
當類可能需要該方法的多個實現時。
在以下情況中使用接口:
-
當存在一組可能被調用的相關方法時。
-
當類只需要方法的單個實現時。
-
當使用接口的類想要將該接口強制轉換為其他接口或類類型時。
-
當正在實現的方法鏈接到類的類型或標識時:例如比較方法:使用單一方法接口而不使用委托的一個很好的示例是 IComparable 或 IComparable。IComparable 聲明 CompareTo 方法,該方法返回一個整數,以指定相同類型的兩個對象之間的小于、等于或大于關系。IComparable 可用作排序算法的基礎,雖然將委托比較方法用作排序算法的基礎是有效的,但是并不理想。因為進行比較的能力屬于類,而比較算法不會在運行時改變,所以單一方法接口是理想的。
五.協變.
class Mammals
{}
class Dogs : Mammals
{}
class Program
{// Define the delegate.public delegate Mammals HandlerMethod();
public static Mammals FirstHandler()
{return null;}
public static Dogs SecondHandler()
{return null;}
static void Main(){HandlerMethod handler1 = FirstHandler;// Covariance allows this delegate.HandlerMethod handler2 = SecondHandler;
}
}
六.逆變
System.DateTime lastActivity;public Form1(){InitializeComponent();
lastActivity = new System.DateTime();
this.textBox1.KeyDown += this.MultiHandler; //works with KeyEventArgs
this.button1.MouseClick += this.MultiHandler; //works with MouseEventArgs
}
// Event hander for any event with an EventArgs or// derived class in the second parameterprivate void MultiHandler(object sender, System.EventArgs e)
{lastActivity = System.DateTime.Now;}
七.合并
delegate void Del(string s);
class TestClass
{static void Hello(string s)
{System.Console.WriteLine(" Hello, {0}!", s);
}
static void Goodbye(string s)
{System.Console.WriteLine(" Goodbye, {0}!", s);
}
static void Main(){Del a, b, c, d;// Create the delegate object a that references// the method Hello:a = Hello;// Create the delegate object b that references// the method Goodbye:b = Goodbye;// The two delegates, a and b, are composed to form c:c = a + b;// Remove a from the composed delegate, leaving d,// which calls only the method Goodbye:d = c - a;System.Console.WriteLine("Invoking delegate a:");
a("A");System.Console.WriteLine("Invoking delegate b:");
b("B");System.Console.WriteLine("Invoking delegate c:");
c("C");System.Console.WriteLine("Invoking delegate d:");
d("D");}
}
輸出
Invoking delegate a: Hello, A! Invoking delegate b: Goodbye, B! Invoking delegate c: Hello, C! Goodbye, C! Invoking delegate d: Goodbye, D!

浙公網安備 33010602011771號