742
京東網上商城
C#委托基礎7——匿名方法
C#委托基礎係列原於2011年2月份發表在我的新浪博客中,現在將其般至本博客。
class Program
{
double AddInt(int x, int y)
{
return x + y;
}
string AddString(string s1, string s2)
{
return s1 + s2;
}
static void Main(string[] args)
{
Program p = new Program();
// 以為前兩個參數為int,他們運行的結果為double,最後一個參數與AddInt返回值一致
Func<int, int, double> funcInt = p.AddInt;
Console.WriteLine("funcInt的值為{0}", funcInt(100, 300));
Func<string, string, string> funcString = p.AddString;
Console.WriteLine("funcString的值為{0}", funcString("aaa", "bbb"));
// 匿名方法
Func<float, float, float> fucFloat = delegate(float x, float y)
{
return x + y;
};
Console.WriteLine("funcFloat的值為{0}", fucFloat(190.7F, 99999.9F));
Console.ReadLine();
}
}
本文參考自金旭亮老師的《.NET 4.0麵向對象編程漫談》有關代理的內容
最後更新:2017-04-03 16:49:33
上一篇:
SQL語句的基本操作
下一篇:
Android開發10——Activity的跳轉與傳值_顯示意圖(intent)的應用
《C++遊戲編程入門(第4版)》——2.6 使用while循環
java泛型學習4之自定義泛型類(DAO)
ibatis中的模煳查詢
幾種排序算法整理
2013年07月26日
Java NIO係列教程(四) Scatter/Gather
httpd: Could not reliably determine the server's fully qualified domain name
oracle中的函數使用
軟件需求
tomcat報錯INFO: Maximum number of threads (200) created for connector with address null and port 8080