深入c# Func委托的详解
更新时间:2013年06月09日 09:45:20 作者:
本篇文章是对c#中的Func委托进行了详细的分析介绍,需要的朋友参考下
如下所示:
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();
}
}
}
相关文章
在Framework 4.0中:找出新增的方法与新增的类(一)
经常看到有同学在讨论Framework 4 的新特性,新方法,于是想写个程序找出framework4.0中新增的方法和类2013-05-05C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案
这篇文章主要介绍了C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-12-12Unity编辑器资源导入处理函数OnPreprocessAudio用法示例
这篇文章主要为大家介绍了Unity编辑器资源导入处理函数OnPreprocessAudio用法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-08-08
最新评论