委托
为什么使用委托?
将一个方法作为参数传递给另一个方法时,使用委托。这个参数方法的类型就叫委托。
声明:关键字delegate,委托与方法相似,但是委托没有方法体。注意:委托必须跟所指向的函数有相同的签名(参数和返回值)。委托需要在命名空间下声明。
delegate void MethodInvoker(int x);
简单的委托示例
namespace ConsoleApp5
{
//声明委托
public delegate double DoubleOp(double x);
public class Program
{
public static void Main(string[] args)
{
double value = 3.3;
Display(MathOperations.Squ, value);
Console.ReadKey();
}
//应用委托的方法 op即需要传入的方法
public static void Display(DoubleOp op,double value)
{
double result = op(value);
Console.WriteLine(result);
}
}
//含有两个不同方法的类
public class MathOperations
{
public static double Mul(double value)
{
return value * 2;
}
public static double Squ(double value)
{
return value * value;
}
}
}
匿名函数写法:
namespace ConsoleApp5
{
//声明委托
public delegate double DoubleOp(double x);
public class Program
{
public static void Main(string[] args)
{
double value = 3.3;
DoubleOp op = delegate (double x)
{
return x * 2;
};
double result=op(value);
Console.WriteLine(result);
Console.ReadKey();
}
}
}
匿名函数的主要体现是:
在实例委托时,DoubleOp op = delegate (double x){return x * 2;};
{}中的内容是对x后加的方法处理,其实相当于委托,在{}可以任意写入方法。
Lambda表达式写法
namespace ConsoleApp5
{
//声明委托
public delegate double DoubleOp(double x);
public class Program
{
public static void Main(string[] args)
{
double value = 3.3;
DoubleOp op = (double x) =>
{
return x * 2;
};
double result=op(value);
Console.WriteLine(result);
Console.ReadKey();
}
}
}
lambda表达式的写法就是吧delegate去掉 换成 =>
泛型委托
namespace ConsoleApp5
{
public delegate bool Operation<T>(T num1, T num2);
public class Program
{
public static void Main(string[] args)
{
string[] vs = { "lokk", "abbie", "ssssss" };
int[] vs1 = { 1, 3, 5, 7, 8 };
string result = GetMax<string> (vs, (string num1, string num2) =>
{
string s1 = (string)num1;
string s2 = (string)num2;
if (s1.Length < s2.Length)
{
return true;
}
else
{
return false;
}
});
Console.WriteLine((string)result);
Console.ReadKey();
}
public static T GetMax<T>(T[] nums, Operation<T> op)
{
T max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
if (op(max, nums[i]))
{
max = nums[i];
}
}
return max;
}
}
}
Action委托
- Action是.Net Framework内置的委托,要求:不带有返回值
Func委托
- Func是内置的代返回值的委托。
事件
事件基于委托,为委托提供了一种发布/订阅机制。
- 自定义委托
class program
{
static void Main(string[] args)
{
Bridegroom bridegroom = new Bridegroom();
Friend friend1 = new Friend("Lok");
Friend friend2 = new Friend("Abbiee");
Friend friend3 = new Friend("Kathy");
bridegroom.MarryEvent += friend1.SengMessage;
bridegroom.MarryEvent += friend2.SengMessage;
bridegroom.OnMarriageComing("朋友们,结婚了");
Console.ReadKey();
}
}
class Bridegroom
{
public delegate void MarryHandler(string msg);
//声明自定义委托类型的事件
public event MarryHandler MarryEvent;
public void OnMarriageComing(string msg)
{
if (MarryEvent != null)
{
MarryEvent(msg);
}
}
}
class Friend
{
public string Name;
public Friend(string name)
{
this.Name = name;
}
public void SengMessage(string msg)
{
Console.WriteLine(Name+"收到了,到时候准时参加");
}
}
- .net 框架下的EventHandler委托:
EventHandler的参数类型是object和EventArg,所以被指向的方法也需要更改成一致的输入参数。
obj:对象,包含事件的发送者。
e:提供了事件的相关信息,且必须派生于EventArgs。
class Program
{
static void Main(string[] args)
{
Bridegroom bridegroom = new Bridegroom();
Friend friend1 = new Friend("Lok");
Friend friend2 = new Friend("Abbiee");
Friend friend3 = new Friend("Kathy");
bridegroom.MarryEvent += friend1.SengMessage;
bridegroom.MarryEvent += friend2.SengMessage;
bridegroom.OnMarriageComing("朋友们,结婚了");
Console.ReadKey();
}
}
class Bridegroom
{
public event EventHandler MarryEvent;
public void OnMarriageComing(string msg)
{
if (MarryEvent != null)
{
MarryEvent(this,new EventArgs());
}
}
}
class Friend
{
public string Name;
public Friend(string name)
{
this.Name = name;
}
public void SengMessage(object obj,EventArgs e)
{
Console.WriteLine(Name+"收到了,到时候准时参加");
}
}
当需要在发布事件是传入参数,可以通过继承EventArgs来实现。
class Program
{
static void Main(string[] args)
{
Bridegroom bridegroom = new Bridegroom();
Friend friend1 = new Friend("Lok");
Friend friend2 = new Friend("Abbiee");
Friend friend3 = new Friend("Kathy");
bridegroom.MarryEvent += friend1.SengMessage;
bridegroom.MarryEvent += friend2.SengMessage;
bridegroom.OnMarriageComing("朋友们,结婚了");
Console.ReadKey();
}
}
//自定义MyEventArgs继承EventArgs
public class MyEventArgs : EventArgs
{
public string _message;
public MyEventArgs(string message)
{
this._message = message;
}
}
class Bridegroom
{
public event EventHandler<MyEventArgs> MarryEvent;
public void OnMarriageComing(string msg)
{
if (MarryEvent != null)
{
//可以传入msg参数
MarryEvent(this,new MyEventArgs(msg));
}
}
}
class Friend
{
public string Name;
public Friend(string name)
{
this.Name = name;
}
public void SengMessage(object obj, MyEventArgs e)
{
Console.WriteLine(e._message);
Console.WriteLine(Name+"收到了,到时候准时参加");
}
}