
namespace Consoleapplication1{ public delegate void methodDelegate(string str); class PRogram { static void Main(string[] args) { Student student = new Student(); Teacher teacher = new Teacher("王老師"); methodDelegate methodDelegate1 = new methodDelegate(student.getStudentName); methodDelegate1 += teacher.getTeacherName; //可以指向不同類中的方法! //methodDelegate1 += teacher.getClassName; 指向簽名不符的方法時提示錯誤! methodDelegate1.Invoke("張三"); Console.ReadLine(); } } class Student { private String name = ""; public Student (String _name) { this.name = _name ; } public Student() {} public void getStudentName(String _name) { if (this.name != "" ) Console.WriteLine("Student's name is {0}", this.name); else Console.WriteLine("Student's name is {0}", _name); } } class Teacher { private String name; public Teacher(String _name) { this.name = _name; } public void getTeacherName(String _name) { if (this.name != "") Console.WriteLine("Teacher's name is {0}", this.name); else Console.WriteLine("Teacher's name is {0}", _name); } public string getClassName() { return "Eanlish"; } }}
上面代碼中
實現對委托的調用
最后將被調用的委托輸出
private delegate String getAString( string parament); static void Main(String []args) { int temp = 40; getAString stringMethod = new getAString(temp.ToString); //傳遞temp.ToString調用委托 Console.WriteLine("String is {0}", stringMethod()); //stringMethod()調用已經接受參數的委托 Console.ReadLine(); }getAString stringMethod = new getAString(temp.ToString); stringMethod += temp.ToString; stringMethod -= temp.ToString;
這種調用之后,按照接受參數的次數, 委托會生成一個列表,每用+=調用一次,會增加一個列表項,-=調用一次,會移除一個列表項。
當我們定義委托 讓委托形成一個數組的時候,我們可以通過遍歷數組的方式來調用它
delegate double Operations(double x); class Program { static void Main() { Operations[] operations = { MathOperations.MultiplyByTwo, MathOperations.Square }; for (int i = 0; i < operations.Length; i++) { Console.WriteLine("Using operations[{0}]:", i); DisplayNumber(operations[i], 2.0); DisplayNumber(operations[i], 7.94); Console.ReadLine(); } } static void DisplayNumber(Operations action, double value) { double result = action(value); Console.WriteLine( "Input Value is {0}, result of operation is {1}", value, result); } } struct MathOperations { public static double MultiplyByTwo(double value) { return value * 2; } public static double Square(double value) { return value * value; } }
上面實例中

將委托定義好
之后就可以遍歷它
利用微軟預先定義的兩個泛型類型來給我們調用


二者都可以接受最大參數個數為16個,二者的區別在于一個有返回值,另外一個返回值為void
實例
using System;namespace DelegateFuncAction{ class Program { static void Main(string[] args) { Func<double, double,double> DoAddtion = calculate.addtion; double result = DoAddtion(20, 30); Console.WriteLine("Func帶返回參數委托做加法結果為:{0}",DoAddtion(10,20)); calculate c=new calculate(); Action<double, double> DoSubstraction = c.substraction; DoSubstraction(90, 20); } } class calculate { public static double addtion(double x, double y) { return x + y; } public void substraction(double x, double y) { Console.WriteLine("Action不帶返回參數委托做減法結果為:{0}",x-y); } }}二者定義的方法名都可以把函數賦予它 如下

匿名方法基本上和上面沒有區別,只是實例化時候不同而已
class Program { static void Main() { double width = 12; Func<double, double> square = delegate(double length) { length *= width; return length; }; Console.WriteLine(square(10)); } }這樣減少了代碼的編寫 將一個函數直接在定義的時候賦予Func<T>或者Action<T>定義的函數
先看Lambda表達式的語法知識

例如
delegate int del(int i);static void Main(string[] args){ del myDelegate = x => x * x; int j = myDelegate(5); //j = 25}實例中
x => x * x就是一個參數為x 執行x*x的lambda的表達式 賦予一個委托
1.用 delegate關鍵詞來聲明(需要聲明返回值,接受參數)
2.Action<T>來聲明一個返回值為void類型的委托
3.Func<T>來聲明有返回類型的委托
無論委托是如何實現的,我們都是賦予它函數時候體現的
新聞熱點
疑難解答