当前位置: 编程技术>.net/c#/asp.net
深入c# Func委托的详解
来源: 互联网 发布时间:2014-10-21
本文导语: 如下所示: 代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Anonymous_Lam{ delegate string ConvertMethod(string Method); class Lambda_Fun { static void Main() { Convert...
如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Anonymous_Lam
{
delegate string ConvertMethod(string Method);
class Lambda_Fun
{
static void Main()
{
ConvertMethod ConvertUpperstring = upperCaseString;
Console.WriteLine("Using delegate instance to call upperString method");
Console.WriteLine(ConvertUpperstring("guohu"));
Console.WriteLine("--------------------");
Console.WriteLine("Using anonymous delegate");
ConvertMethod ConvertAsynCaseString = delegate(string s)
{
return s.ToUpper();
};
Console.WriteLine(ConvertAsynCaseString("leihu"));
Console.WriteLine("--------------------");
Console.WriteLine("Using Func");
Func FuncCaseUpper = upperCaseString;
Console.WriteLine(FuncCaseUpper("junwenLi"));
Console.WriteLine("--------------------");
Console.WriteLine("Using anonymous Func");
Func FuncAnonyCaseUpper = delegate(string Name)
{
return Name.ToUpper();
};
Console.WriteLine(FuncAnonyCaseUpper("jinhaoLiu"));
Console.WriteLine("--------------------");
Console.WriteLine("Using lambda Expression");
Func FuncLambda = Name => Name.ToUpper();
Console.WriteLine(FuncLambda("chengfan"));
}
static string upperCaseString(string strName)
{
return strName.ToUpper();
}
}
}
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Anonymous_Lam
{
delegate string ConvertMethod(string Method);
class Lambda_Fun
{
static void Main()
{
ConvertMethod ConvertUpperstring = upperCaseString;
Console.WriteLine("Using delegate instance to call upperString method");
Console.WriteLine(ConvertUpperstring("guohu"));
Console.WriteLine("--------------------");
Console.WriteLine("Using anonymous delegate");
ConvertMethod ConvertAsynCaseString = delegate(string s)
{
return s.ToUpper();
};
Console.WriteLine(ConvertAsynCaseString("leihu"));
Console.WriteLine("--------------------");
Console.WriteLine("Using Func");
Func FuncCaseUpper = upperCaseString;
Console.WriteLine(FuncCaseUpper("junwenLi"));
Console.WriteLine("--------------------");
Console.WriteLine("Using anonymous Func");
Func FuncAnonyCaseUpper = delegate(string Name)
{
return Name.ToUpper();
};
Console.WriteLine(FuncAnonyCaseUpper("jinhaoLiu"));
Console.WriteLine("--------------------");
Console.WriteLine("Using lambda Expression");
Func FuncLambda = Name => Name.ToUpper();
Console.WriteLine(FuncLambda("chengfan"));
}
static string upperCaseString(string strName)
{
return strName.ToUpper();
}
}
}