深入c# Func委托的详解
程序员文章站
2023-12-19 19:46:22
如下所示:复制代码 代码如下:using system;using system.collections.generic;using system.linq;using s...
如下所示:
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<int t,out tresult>");
func<string, string> funccaseupper = uppercasestring;
console.writeline(funccaseupper("junwenli"));
console.writeline("--------------------");
console.writeline("using anonymous func<int t,out tresult>");
func<string, string> funcanonycaseupper = delegate(string name)
{
return name.toupper();
};
console.writeline(funcanonycaseupper("jinhaoliu"));
console.writeline("--------------------");
console.writeline("using lambda expression");
func<string, string> 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<int t,out tresult>");
func<string, string> funccaseupper = uppercasestring;
console.writeline(funccaseupper("junwenli"));
console.writeline("--------------------");
console.writeline("using anonymous func<int t,out tresult>");
func<string, string> funcanonycaseupper = delegate(string name)
{
return name.toupper();
};
console.writeline(funcanonycaseupper("jinhaoliu"));
console.writeline("--------------------");
console.writeline("using lambda expression");
func<string, string> funclambda = name => name.toupper();
console.writeline(funclambda("chengfan"));
}
static string uppercasestring(string strname)
{
return strname.toupper();
}
}
}