C# 中常用内置委托
Action
,Func
,EventHandler
和EventHandler
介绍
封装一个没有返回值的方法
public delegate void Action();
样例
public static void Main()
{
Action showMethod = DisplayToConsole;
showMethod();
Console.ReadKey();
}
static void DisplayToConsole()
{
Console.WriteLine("hello");
}
封装一个没有返回值有一个参数的方法
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个参数,但所有方法均无返回值。
代表有一个 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";
}
代表有一个 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
的返回值。
表示将处理没有事件数据的事件方法。
[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;
}
}
表示将处理包含事件数据的事件方法。
[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; }
}
}