C# 中的 Action, Func, EventHandler 和 EventHandlerTEventArgs
C# 中常用内置委托
Action
,Func
,EventHandler
和EventHandler<TEventArgs>
介绍
Action Delegate
封装一个没有返回值的方法
public delegate void Action();
样例
public static void Main(){Action showMethod = DisplayToConsole;showMethod();Console.ReadKey();}static void DisplayToConsole(){Console.WriteLine("hello");}
Action <T > Delegate
封装一个没有返回值有一个参数的方法
public delegate void Action<in T>(T obj);
样例
public static void Main(){Action<string> messageTarget = ShowMessage;messageTarget("Hello, World!");Console.ReadKey();}static void ShowMessage(string message){Console.WriteLine(message);}
其它 Action
委托
Action 最多可以支持16个参数,但所有方法均无返回值。
Func <TResult > Delegate
代表有一个 TResult
类型的返回值没有参数的方法
public delegate TResult Func<out TResult>();
样例
static void Main(string[] args){Func<string> func = GetValue;string value = func();Console.WriteLine(value);Console.ReadKey();}static string GetValue(){return "hello";}
Func <T,TResult > Delegate
代表有一个 TResult
类型的返回值,有一个 T
类型参数的方法
public delegate TResult Func<in T,out TResult>(T arg);
样例
static void Main(string[] args){Func<int,string> func = GetValue;string value = func(10);Console.WriteLine(value);Console.ReadKey();}static string GetValue(int num){int count=0;for (int i = 0; i < num; i++){count += i;}return count.ToString();}
其它 Func
委托
Func 最多可以支持16个参数,但所有方法均有一个类型为 TResult
的返回值。
EventHandler Delegate
表示将处理没有事件数据的事件方法。
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public delegate void EventHandler(object sender, EventArgs e);
参数
sender
:事件来源
e
:不包含事件数据的对象
样例
using System;namespace ConsoleApplication1
{class Program{static void Main(string[] args){Counter c = new Counter(new Random().Next(10));c.ThresholdReached += c_ThresholdReached;Console.WriteLine("press 'a' key to increase total");while (Console.ReadKey(true).KeyChar == 'a'){Console.WriteLine("adding one");c.Add(1);}}static void c_ThresholdReached(object sender, EventArgs e){Console.WriteLine("事件被触发");Console.ReadKey();}}class Counter{private int threshold;private int total;public Counter(int passedThreshold){threshold = passedThreshold;}public void Add(int x){total += x;if (total >= threshold){if (ThresholdReached != null){ThresholdReached(this, null);}}}public event EventHandler ThresholdReached;}}
EventHandler <TEventArgs > Delegate
表示将处理包含事件数据的事件方法。
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
参数
TEventArgs
:事件数据类型
sender
:事件来源
e
:包含事件数据的对象
样例
using System;namespace ConsoleApplication1
{class Program{static void Main(string[] args){Counter c = new Counter(new Random().Next(10));c.ThresholdReached += c_ThresholdReached;Console.WriteLine("press 'a' key to increase total");while (Console.ReadKey(true).KeyChar == 'a'){Console.WriteLine("adding one");c.Add(1);}}static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e){Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);Environment.Exit(0);}}class Counter{private int threshold;private int total;public Counter(int passedThreshold){threshold = passedThreshold;}public void Add(int x){total += x;if (total >= threshold){ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();args.Threshold = threshold;args.TimeReached = DateTime.Now;OnThresholdReached(args);}}protected virtual void OnThresholdReached(ThresholdReachedEventArgs e){EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;if (handler != null){handler(this, e);}}public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;}public class ThresholdReachedEventArgs : EventArgs{public int Threshold { get; set; }public DateTime TimeReached { get; set; }}
}
C# 中的 Action, Func, EventHandler 和 EventHandlerTEventArgs相关推荐
- 如何使用 C# 中的 Action, Func,Predicate
译文链接:https://www.infoworld.com/article/3057152/how-to-work-with-action-func-and-predicate-delegates- ...
- 浅谈C#中常见的委托Func,Action,Predicate(转)
一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...
- c# 带返回值的action_C#知识点讲解之C#delegate、event、Action、EventHandler的使用和区别...
今天来讲一讲<C#delegate.event.Action.EventHandler的使用和区别> 目录 所以,event应运而生 所以,EventHandler应运而生 所以,Acti ...
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...
- spark的RDD中的action(执行)和transformation(转换)两种操作中常见函数介绍
参考文章:spark的RDD中的action(执行)和transformation(转换)两种操作中常见函数介绍 spark常见的RDD 1. 函数概览 2. 常见的Transformations 操 ...
- 【wpf】自定义事件总结(Action, EventHandler)
Action和EventHandle Action和EventHandler都是.net帮我定义好的委托,我们可以直接使用,方便的定义事件. 利用Action定义事件 //无参事件 event Act ...
- Struts2中的action类
Struts2中的action类 action类在Struts2中承担了Model(模型)的角色,主要用于处理业务逻辑并存放HTTP请求处理过程中各个变量的值. 在Struts2里面,Action充当 ...
- Struts2中的Action
多数的MVC框架中的Control层,都是一个Java对象.按照惯例,我们通常会把这个层次上面的Java对象统称为Action层.本篇文章,我们就来简单介绍一下Struts2中Action的相关内容. ...
- 【struts2】struts2中的Action详解
在传统的MVC框架(如struts1.Spring等)中,Action都需要实现特定的接口,这些接口都是MVC框架定义的,实现MVC的接口会与MVC框架耦合.struts2的Action要灵活得多,可 ...
最新文章
- 如何从Subversion存储库中git-svn克隆最后n个修订版?
- android 日历按周获取,Android日历获取当前周,前几周和下周
- 网站内容页面如何优化才利于排名提升?
- 05-移动端开发教程-CSS3兼容处理
- ubuntu shell实现加减乘除
- 小程序服务器七牛云,基于七牛云 API 开发的微信小程序 SDK
- AcWing 1230. K倍区间
- [react] React为什么要搞一个Hooks?
- 云存储技术-Zookeeper集群的安装
- Java应用线上CPU飙高
- “深度学习已死,可微编程万岁!”LeCun老师为何又语出惊人?
- 创建单IP的×××网络
- 国家信息安全水平考试NISP一级模拟题
- html中怎样把背景图片置顶,HTML5中背景图片如何设置
- win10计算机磁盘图标,主编教你win10系统无法正常显示硬盘图标的方法
- iis发布网站时出现根目录文件解决方案
- 内网禁用u盘 远程协助_如何在Windows 10中禁用远程协助
- c语言括号里三种字符,c语言的基本数据类型都有什么,麻烦知道用大括号分类,非常感谢...
- 安卓仿陌陌用户详情页轮播图联动效果
- zookeepr 简介