C # entrusted supplement 01

The last article wrote some of the most basic things for commissioning. In this article, let’s talk about commissioning other things.

Example 1 plug-in programming

According to the understanding of the delegation, the delegation can Pass a method as a parameter. Using this feature, we can use delegates to implement plug-in programming.

public delegate int Transformer(int x);

class Util{
pubilc static void Transform(int[] values, Transformer t){
for (int i = 0; i values[i] = t?.Invke(values[i]);
}
}
}

class Test{
static void Main(){
int[] values ​​= {1,2,3};
Util.Transform(values, Sqare) ;

foreach(int i in values){
Console.WriteLine(i + ""); //1 4 9
}
}

static int Sqare (int x) => x * x;
}

The Transform method here is a plug-in method, which accepts a delegate parameter.

Example 2 Multicast delegation

All delegations have multicast capabilities. This means that a delegate can refer to a method or a group of methods. The delegate uses += and -= operators to connect multiple delegate instances.

public class People{}
public class Student:People{}
public delegate void Action();

class Program
{< br /> static void Say(){
Console.WriteLine("Say HI");
}
static void Write(){
Console.WriteLine("Write Anything" );
}

Action action = Say;
action += Write;//action = action + Write;
action();
< br /> /*
Say HI
Write Anything
*/



}

The commission will follow The order of addition is triggered one by one. If there is a return value, then the caller will return the last method return value with a non-void return value type, and other return values ​​will be discarded.

-and -= operators will delete the delegate operation on the right from the delegate operand on the left.
Performing a + or += operation for a delegate whose value is null is equivalent to assigning a new value to the delegate. Also when the delegate has only one target method, executing the -= operation is equivalent to specifying a null value for the delegate.
Since the delegate is a class, the delegate is immutable. Each execution of += or -= is essentially equivalent to creating a new delegate instance and assigning a value to it.
For specific use, for example, we need to build a building, which will take a long time, but the company requires that each floor be built need to be reported to the company.

public delegate void ProgressReporter(int floor);

public class Edifice{
public static void Build(ProgressReporter p){
for(int i = 0; i <18; i++){
p(i);

System.Threading.Thread.Sleep(1000);//Add some time
}
}
}
class Test{
static void Main(){
ProgressReporter p = WriteProgressToConsole;
p += AdviseToCompany;
Edifice.Build( p);
}

static void WriteProgressToConsole(int fool) => Console.WriteLine($"{fool} layer has been built");

static void AdviseToCompany(int fool) => Console.WriteLine($"**Project, {fool} layer has been built.");


}

Example 3 Generic delegate

The delegate type can contain generic type parameters, such as:

public delegate T Transformer (T t);

Through a generic parameter, we can define a general Transformer and make it valid for any type:

public class util{< br /> public static void Transform (T[] values, Tra nsformer t){
for(int i = 0; i values[i] = t(values[i]);
}< br /> }
}
class Test{
static void Main(){
int[] values ​​= {1,2,3};
Util.Transformer (values, Square);

foreach(int i in values){
Console.Write(i + ""); //1 4 9
}
}

static int Sqare (int x) => x * x;

}

Since delegates support generic parameters, you can define some Small commission types, they can have any return type and a reasonable number of parameters, such as system-defined Action commission and Func commission (out and in mark variability modifiers, which are explained in contravariance and covariance) .

delegate void Action();
delegate void Action (T arg);
delegate void Action (T1 arg1, T2 arg2)
………
delegate TResule Func(out TResule)();
delegate TResule Func (T arg);
delegate TResule Func (T1 arg1, T2 arg2)
………

Among them, Action and Func can support 16 parameters.
The above Transform rewrite.

public static void Transform (T[] values, Func transformer){
for(int i = 0; i values[i] = transformer(values[i]);
}
}

There are still many things to entrust, let’s talk next time!

Reference from C#7.0 Core Technology Guide

Leave a Comment

Your email address will not be published.