泛型委托Action与func
来源:互联网 发布:mac怎么下photoshop 编辑:程序博客网 时间:2024/05/21 09:14
泛型委托Action<T>和Fun<TResult>两类特殊的委托,这两个特殊的委托是Dot FrameWrok自带的。结合lambda表达式,可以在写程序时,简洁代码和提高编码效率。
(一)Action<T>和Fun<TResult>两个委托的不同点:
Action<T>只能委托必须是无返回值的方法
Fun<TResult>只是委托必须有返回值的方法
namespace ConsoleApplication1{ public delegate void ShowName(); public delegate void ShowNameWithParameter(string name); public delegate void ShowAge(int age); public delegate void ShowNameAndAge(string name, int age); public delegate int ReturnName(string name); public class Person { #region 开始 private string instanceName; //姓名 public string InstanceName { get { return instanceName; } set { instanceName = value; } } private int instanceAge; //年龄 public int InstanceAge { get { return instanceAge; } set { instanceAge = value; } } public Person(string name, int age) //构造函数 { this.instanceName = name; this.instanceAge = age; } public void DisplayName() //输出姓名 { Console.WriteLine("Name:{0}", this.instanceName); } public void DisplayName(string name) //输出姓名 { Console.WriteLine("Name:{0}", name); } public void DisplayAge(int age) //输出姓名 { Console.WriteLine("Age:{0}", age); } public void DisplayNameAndAge(string name, int age) //输出姓名和年龄 { Console.WriteLine(string.Format("Name:{0} And Age:{1} ", name, age)); } public int GetAgeByName(string name) //根据姓名获得年龄 { if (name == instanceName) { return instanceAge; } else { return -1; } } #endregion } class Program { private static void Print(string s) { Console.WriteLine(s); } static void Main(string[] args) { #region Action<T>相关 Person person = new Person("joetao", 21); //非泛型委托 //ShowName showName = new ShowName(name.DisplayName); //另一种写法 ShowName showName = person.DisplayName; showName(); ShowNameWithParameter showNameWithParameter = person.DisplayName; showNameWithParameter(person.InstanceName); ShowAge showAge = person.DisplayAge; showAge(person.InstanceAge); ShowNameAndAge showNameAndAge = person.DisplayNameAndAge; showNameAndAge(person.InstanceName, person.InstanceAge); //泛型委托Action<T,...>这里指多个类型参数 //Action<T,..>委托:只能委托无返回值(void)的方法。 //(2)可以使用此委托以参数形式传递方法,而不用显式声明自定义的委托 Action actionShowName = person.DisplayName; actionShowName(); Action<string> actionShowName1 = person.DisplayName; actionShowName1(person.InstanceName); Action<int> actionShowAge = person.DisplayAge; actionShowAge(person.InstanceAge); Action<string, int> actionShowNameAndAge = person.DisplayNameAndAge; actionShowNameAndAge(person.InstanceName, person.InstanceAge); //匿名泛型委托 //(1)初步 Action<string> actionPrintNameAtFirst; actionPrintNameAtFirst = delegate(string s) { Console.WriteLine("Name:{0}", s); }; actionPrintNameAtFirst(person.InstanceName); //(2)简化 Action<string> actionprintName; actionprintName = s => Console.WriteLine("Name:{0}", s); actionprintName(person.InstanceName); //示例演示如何使用 Action<T> 委托来打印 List<T> 对象的内容 //使用 Print 方法将列表的内容显示到控制台上。 此外,C# 示例还演示如何使用匿名方法将内容显示到控制台上 //请注意该示例不显式声明 Action<T> 变量。 相反,它传递方法的引用,该方法采用单个参数而且不将值返回至 List<T>.ForEach 方法,其单个参数是一个 Action<T> 委托 // 同样,在 C# 示例 中,Action<T> 委托不被显式地实例化,因为匿名方法的签名匹配 List<T>.ForEach 方法所期望的 Action<T> 委托的签名。 List<String> names = new List<String>(); names.Add("Bruce"); names.Add("Alfred"); names.Add("Tim"); names.Add("Richard"); names.ForEach(Print); names.ForEach(delegate(String name) { Console.WriteLine(name); }); #endregion #region Fun<TResultf>相关 //泛型委托Func<T,...,TResult> 委托,这里可以有一个或多个或者没有参数T,但必须有返回值并返回 TResult 参数所指定的类型的值 //(1)必须有指定参数返回值。 //(2)Lambda 表达式的基础类型是泛型 Func 委托之一。 这样能以参数形式传递 lambda 表达式,而不用显式将其分配给委托。 //(3)因为 System.Linq 命名空间中许多类型方法具有 Func<T, TResult> 参数,因此可以给这些方法传递 lambda 表达式,而不用显式实例化 Func<T, TResult> 委托。 //非泛型委托实现 ReturnName returnName = person.GetAgeByName; Console.WriteLine("Age:{0}", returnName(person.InstanceName)); //泛型委托实现 Func<string, int> funReturnName = person.GetAgeByName; Console.WriteLine("Age:{0}", funReturnName(person.InstanceName)); //泛型匿名委托实现 //(1)初步 Func<string, int> funReturnName1 = delegate(string s) { return person.GetAgeByName(s); }; Console.WriteLine("Age:{0}", funReturnName1(person.InstanceName)); //(2)优化 Func<string, int> funReturnName2 = s => person.GetAgeByName(s); Console.WriteLine("Age:{0}", funReturnName2(person.InstanceName)); //示例演示如何声明和使用 Func<T, TResult> 委托 //此示例声明一个 Func<T, TResult> 变量,并为其分配了一个将字符串中的字符转换为大写的 lambda 表达式。 //随后将封装此方法的委托传递给 Enumerable.Select 方法,以将字符串数组中的字符串更改为大写。 Func<string, string> selector = str => str.ToUpper(); string[] words = { "orange", "apple", "Article", "elephant" }; IEnumerable<String> aWords = words.Select(selector); foreach (String word in aWords) Console.WriteLine(word); #endregion Console.ReadKey(); } }}
0 0
- 泛型委托Action与func
- Func与Action委托泛型介绍
- Action与Func委托
- 委托--Func与Action
- Func<T>与Action<T>委托泛型介绍
- 泛型委托System.Func<>与System.Action<>
- Func<T>与Action<T>委托泛型介绍
- 泛型委托 Predicate/Func/Action
- 泛型委托学习(Func和Action)
- C# 泛型委托 Action/Func
- 泛型委托 Predicate/Func/Action
- Action 和 Func 泛型封装委托
- 学习笔记之Func<T>与Action<T>委托泛型介绍
- 委托——Func与Action
- 泛型委托 Predicate/Func/Action ,c#扩展方法
- C#的泛型委托Predicate/Func/Action
- Action、Func、Predicate 和 Converter 泛型委托详解
- Action<T>和Func<T>泛型委托
- JSON数据处理及与echarts结合使用时所需要的问题
- 使用XML布局文件和java代码共同控制UI界面做一个简易图片浏览器
- 使用hibernate获取自增主键的id值
- SSH框架开发流程步骤
- 第十三周--形状类族的中的纯虚函数
- 泛型委托Action与func
- 《图解HTTP》学习笔记(四)-返回结果的HTTP状态码
- unity3d 动作会产生抖动的问题
- Ajax异步交互解决数据乱码问题
- READING NOTE: Chained Predictions Using Convolutional Neural Networks
- 检查是否为BST
- 山东省第六届ACM省赛题——Circle of Friends(强连通分量+dfs)
- 异步请求后台json传回前台406
- 【论文】New baseline correction algorithm for text-line recognition with bidirectional recurrent neural