c#委托的概念
Delegate is like a function pointer in c++ means it holds the reference or address of the different methods. Those references can be changed runtime. We have to pass method name as the parameter at the time of instantiation.
委托就像c ++中的函数指针一样,意味着它拥有不同方法的引用或地址。 这些引用可以在运行时更改。 在实例化时,我们必须将方法名称作为参数传递。
Delegates are immutable. It means every time new object is created and fresh memory is allocated.
代表们是一成不变的。 这意味着每次创建新对象并分配新内存时。
Types of delegate 代表类型
There are two types of delegates.
有两种类型的代表。
- Single cast delegate – Delegate holds the reference of single method only 单一强制委托-委托仅保留单一方法的引用
- Multi cast delegate – Delegate holds the references of multiple methods 多类型委托-委托持有多个方法的引用
How to declare or define delegate? 如何声明或定义委托?
For example, we want to define the delegate which holds the reference of methods having string as return type and integer as input parameter then below will be the c# syntax for it.
例如,我们要定义一个委托,该委托持有以字符串为返回类型,整数为输入参数的方法的引用,则下面将是它的c#语法。
internal delegate string MyTestDelegate(int InputValue);
内部委托字符串MyTestDelegate(int InputValue);
Any method which matches the above delegate type can be assigned to this MyTestDelegate delegate.
可以将与上述委托类型匹配的任何方法分配给此MyTestDelegate委托。
How to create object of a delegate / instantiate delegate? 如何创建委托/实例化委托的对象?
After declaring the delegate, we need to create the object of that delegate to assign the method as the parameter. For reference, I have created two methods NumberBetween1To10() and NumberBetween11To20() which takes integer value as input and return string parameter.
声明委托后,我们需要创建该委托的对象以将方法分配为参数。 作为参考,我创建了两个方法NumberBetween1To10()和NumberBetween11To20(),它们将整数值作为输入并返回字符串参数。
private string NumberBetween1To10(int InputValue)
{
string strNumberInWords = string.Empty;
if (InputValue >= 1 && InputValue <= 10)
{
strNumberInWords = "Number is inbetween 1 to 10";
}
return strNumberInWords;
}
private string NumberBetween11To20(int InputValue)
{
string strNumberInWords = string.Empty;
if (InputValue >= 11 && InputValue <= 20)
{
strNumberInWords = "Number is inbetween 11 to 20";
}
return strNumberInWords;
}
On click of one button, we’ll call these two methods synchronously using delegate.
单击一个按钮后,我们将使用委托同步调用这两种方法。
private void btnSingleCast_Click(object sender, EventArgs e)
{
MyTestDelegate objMyTestDel = new MyTestDelegate(NumberBetween1To10);
objMyTestDel.Invoke(9);
objMyTestDel = new MyTestDelegate(NumberBetween11To20);
objMyTestDel.Invoke(15);
}
In above example, we are creating the object objMyTestDel of MyTestDelegate type. To call those methods through delegate, we have to use Invloke() method of that delegate. Here both the methods will be executed one by one.
在上面的示例中,我们正在创建MyTestDelegate类型的对象objMyTestDel。 要通过委托调用这些方法,我们必须使用该委托的Invloke()方法。 这两种方法将一一执行。
DelegateObject.Invoke() method will take the method parameters as argument.
DelegateObject.Invoke()方法将方法的参数作为参数。
Now you might have a question that, we can call these two methods directly by calling them on button click then what is the use of delegate over here.
现在您可能有一个问题,我们可以通过在单击按钮时调用它们来直接调用这两个方法,然后在这里使用委托的作用是什么。
What are the benefits of using delegate? 使用委托有什么好处?
- Useful to call methods synchronously (sequential) as well as asynchronously (parallel). 对于同步(顺序)和异步(并行)调用方法很有用。
- Callback method can be implemented 回调方法可以实现
- Multiple methods can be invoked using one delegate. 可以使用一个委托调用多种方法。
Asynchronous execution of method through delegate 通过委托异步执行方法
In above example, we have executed methods using Invoke() method of the delegate. To call the methods asynchronously, we have to use the BeginInvoke() method of the delegate.
在上面的示例中,我们使用委托的Invoke()方法执行了方法。 要异步调用方法,我们必须使用委托的BeginInvoke()方法。
BeginInvoke() method will take input parameter, ASyncCallBack object, and object as argument.
BeginInvoke()方法将使用输入参数,ASyncCallBack对象和object作为参数。
MyTestDelegate objMyTestDel = new MyTestDelegate(NumberBetween1To10);
objMyTestDel.BeginInvoke(7, new AsyncCallback(CallBackMethod4NumBtwn1To10), null);
objMyTestDel = new MyTestDelegate(NumberBetween11To20);
objMyTestDel.BeginInvoke(15, new AsyncCallback(CallBackMethod4NumBtwn1To10), null);
In above example, execution of first method will be started and after that second will be started. Second method will not wait for first method to complete the execution.
在上面的示例中,第一个方法的执行将开始,第二个方法之后将开始。 第二种方法不会等待第一种方法完成执行。
Callback method implementation 回调方法的实现
In above example, we came to know the parallel execution of the methods. In that code, we passed the callback method name in BeginInvole() method. Callback method will be executed after the completion of that particular method.
在上面的示例中,我们知道了方法的并行执行。 在该代码中,我们在BeginInvole()方法中传递了回调方法名称。 回调方法将在完成该特定方法后执行。
private void CallBackMethod4NumBtwn1To10(IAsyncResult CallBackResult)
{
Console.WriteLine("Execution completed for NumberBetween1To10");
}
private void CallBackMethod4NumBtwn11To20(IAsyncResult CallBackResult)
{
Console.WriteLine("Execution completed for NumberBetween11To20");
}
When execution of NumberBetween1To10 is completed, the control comes to CallBackMethod4NumBtwn1To1
完成NumberBetween1To10的执行后,控件进入CallBackMethod4NumBtwn1To1 0,将执行用该方法编写的代码。 对其他方法将执行相同的方法。
Multicast Delegate 组播代表
Multicast delegate is to call multiple methods using single delegate. Here delegate holds the references of multiple methods. Methods can be registered or unregistered using += and -= operators. In multicast delegate, all registered methods will be called with the same input parameter.
多播委托是使用单个委托调用多个方法。 在这里,委托持有多个方法的引用。 可以使用+ =和-=运算符来注册或取消注册方法。 在多播委托中,将使用相同的输入参数调用所有已注册的方法。
MyTestDelegate objMyTestDel = new MyTestDelegate(NumberBetween1To10);
objMyTestDel += new MyTestDelegate(NumberBetween11To20);
objMyTestDel.Invoke(10);
In above code snippet, two methods NumberBetween1To10() and NumberberBetween11To20() are registered with single delegate object objMyTestDel. Through single object of that delegate, both the methods will be called synchronously one after by another. Here the same parameter 10 will be passed to both the methods as input.
在上面的代码片段中,向单个委托对象objMyTestDel注册了两个方法NumberBetween1To10()和NumberberBetween11To20()。 通过该委托的单个对象,两个方法将被一个接一个地同步调用。 在这里,将相同的参数10作为输入传递给这两种方法。
Asynchronous call is not possible in multicast delegate.
在多播委托中不可能进行异步调用。
MyTestDelegate objMyTestDel = new MyTestDelegate(NumberBetween1To10);
objMyTestDel += new MyTestDelegate(NumberBetween11To20);
objMyTestDel += new MyTestDelegate(NumberBetween1To10);
objMyTestDel.Invoke(10);
If we run the above code snippet, the methods are executed in following manner.
如果我们运行上面的代码片段,则这些方法将以以下方式执行。
NumberBetween1To10()--> NumberBetween11To20() --> NumberBetween1To10()
NumberBetween1To10()-> NumberBetween11To20()-> NumberBetween1To10()
MyTestDelegate objMyTestDel = new MyTestDelegate(NumberBetween1To10);
objMyTestDel += new MyTestDelegate(NumberBetween11To20);
objMyTestDel += new MyTestDelegate(NumberBetween1To10);
objMyTestDel -= new MyTestDelegate(NumberBetween11To20);
objMyTestDel.Invoke(10);
In above code snippet, we are unregistering the method from the delegate using -= operator. The methods will be executed in following manner.
在上面的代码片段中,我们正在使用-=运算符从委托中注销该方法。 该方法将以以下方式执行。
NumberBetween1To10() --> NumberBetween1To10()
NumberBetween1To10()-> NumberBetween1To10()
KeyConceptsOfDelegateUsingC-.doc KeyConceptsOfDelegateUsingC-.doc
翻译自: https://www.experts-exchange.com/articles/28440/Key-concepts-of-Delegate-using-C.html
c#委托的概念