C#的Action和Func

兩個都是委託【代理】的簡寫形式。

public delegate void myDelegate(string str);  
public static void HellowChinese(string strChinese)  
{  
    Console.WriteLine("Good morning," + strChinese);  
    Console.ReadLine();  
}  
  
myDelegate myDele = new myDelegate(HellowChinese);  
myDele("Mr kyo");  


1.action<>指定那些只有輸入參數,沒有返回值的委託

public static void HellowChinese(string strChinese)  
{  
    Console.WriteLine("Good morning," + strChinese);  
    Console.ReadLine();  
}  
Action<string> action = HellowChinese;  
action("Mr kyo"); 


2.func<> 這個和上面的那個是一樣的,區別是這個有返回值!

public static string HelloEnglish(string strEnglish)  
{  
    return "Hello." + strEnglish;  
}  
  
Func<string, string> myFunc = HelloEnglish;  
Console.WriteLine(myFunc("Mr kyo"));  
Console.ReadLine();
 

發佈了123 篇原創文章 · 獲贊 2 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章