【Azure 環境】Update-MgEntitlementManagementAccessPackageAssignmentPolicy 命令執行時候遇見的 No HTTP Resource was found 問題分析
阿新 • • 發佈:2021-09-28
委託也是一種型別,初始化時需要一個方法支援。委託是記錄方法資訊的一種型別,呼叫委託時就是呼叫初始化委託的方法。當委託作為函式引數時,就是將方法作為引數傳入,而這種方法可通過Lambda表示式對進行改寫,可極大加快程式設計的靈活性。
簡單委託
class Program { static void Main(string[] args) { // 例項化委託 傳入委託方法的方法名 可以不用new DelegateHello delegateHello = new DelegateHello(Hello); // 呼叫委託 // delegateHello.Invoke("委託呼叫"); delegateHello("委託呼叫"); } // 定義委託方法 需要與委託返回值、引數型別相同 static void Hello(string msg) { Console.WriteLine("Hello,"+msg); } } // 定義委託 和定義介面中的方法一樣,只需要加上 delagate 關鍵字 delegate void DelegateHello(string msg);
泛型委託
可定義一個委託,用來呼叫多個方法。
class Program { static void Main(string[] args) { // 直接傳遞方法名 不用new // DelegateAdd<int> delegateAddInt = new DelegateAdd<int>(Add); DelegateAdd<int> delegateAddInt = Add; delegateAddInt(1, 1); DelegateAdd<double> delegateAddDouble = Add; delegateAddDouble(1.5, 1.6); } static void Add(int num1,int num2) { Console.WriteLine(num2+num1); } static void Add(double num1, double num2) { Console.WriteLine(num2 + num1); } } // 定義泛型委託 delegate void DelegateAdd<t>(T num1, T num2);
預定義委託
不用事先定義委託就可以使用方法對委託進行初始化,在.net Core中被大量使用。
class Program { static void Main(string[] args) { // 無返回值 16個 //Action<string, string=""> action = new Action<string, string="">(ConcatStr); Action<string, string=""> action = ConcatStr; action("hello","world"); // 有返回值 17個 前面的string表示輸入引數型別 後面最後一個string表示返回結果型別 //Func<string,string> function = new Func<string,string>(PrintStr); Func<string,string> function = PrintStr Console.WriteLine(function("hello")); // Lambda表示式改寫 Action<string> action1 = new Action<string>(msg => Console.WriteLine(msg)); action1("Lambda表達改寫委託"); } static void ConcatStr(string str1,string str2) { Console.WriteLine(str1+str2); } static string PrintStr(string str) { return str; } }
</string,string></string,string></string,string></string,></string,></string,>