A detailed explanation of the common use of built in system delegates in C

  • 2020-10-23 20:15:55
  • OfStack

In general, C# has built in a common language runtime (CLR) environment a number of commonly used delegates, including Action class delegates, Func class delegates, Predicate < T > Entrust, Comparison < T > Delegate and so on. These delegates are all named System and belong to an assembly called ES14en.dll. This article will talk about how to use these delegates.

To implement some functions, as we have already defined, we can instantiate them directly using the system's built-in delegates, rather than having to explicitly define a new delegate and assign naming methods to that delegate. Such as:


public static void Test()
{
  Console.WriteLine("Just For Test");
}
static void Main(string[] args)
{
  Action a = new Action(Test); // Direct instantiation 1 a Action Delegate, you don't have to define a new delegate yourself 
  a();
}

As long as you know what the built-in delegate does, what parameters are passed, and what values are returned, you can follow the example above and call it yourself. The following is my summary of these four types of delegates, with an example of combining anonymous methods with Lambda expressions. The result of the two methods is one example, which you can use flexibly.

1. Delegate to the Action class

1. The Action delegate encapsulates a method that takes no arguments and returns no values

2.Action < T > The delegate encapsulates a method that has only one argument and does not return a value

3.Action < T1,T2 > The delegate encapsulates a method that takes two arguments and returns no value

... ...

17.Action < T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16 > The delegate encapsulates a method that takes 16 parameters and returns no value

The following to Action < T > The delegate example demonstrates how to use the delegate of the Action class, which is simply different in the number of parameters.


static void Main(string[] args)
{
  #region Action<T> Entrusted sample 
  // Requirement: Print out the set of integers list The elements of the 
  List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
  // Assign anonymous methods to  Action<T>  Delegate instance 
  Action<int> concat1 = delegate(int i) { Console.WriteLine(i); };
  list.ForEach(concat1);
  // will  lambda  Expression assigned to  Action<T>  Delegate instance 
  Action<int> concat2 = (i => Console.WriteLine(i));
  list.ForEach(concat2);
  Console.ReadKey();
  #endregion 
}

Conclusion:

The delegate of the Action class can pass in a minimum of 0 parameters and a maximum of 16 parameters, all of which are contravariant and do not return values.

2. Delegate for Func class

1.Func(TResult) delegate wrapper encapsulates a method that has no arguments but returns the type value specified by the TResult argument

2.Func(T,TResult) delegate encapsulates a method with 1 argument and returns a value of the type specified by the TResult argument

3.Func(T1,T2,TResult) delegate encapsulates a method with two arguments and returns the type value specified by the TResult argument

... ...

17.Func < T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,TResult > The delegate encapsulates a method with 16 parameters and returns the value of the type specified by the TResult parameter

The following to Func < T,TResult > For example, show how to use the delegate of the Func class, which is simply different in the number of parameters.


static void Main(string[] args)
{
  #region Func<T,TResult> Entrusted sample 
  // Requirement: Find an integer set list In more than 3 All of the elements that make up the new set, and print out the set elements 
  List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
  // Assign anonymous methods to  Func<T,TResult>  Delegate instance 
  Func<int, bool> concat1 = delegate(int i) { return i > 3; };
  var newlist1 = list.Where(concat1).ToList();
  // will  Lambda  Expression assigned to  Func<T,TResult>  Delegate instance 
  Func<int, bool> concat2 = i => i > 3;
  var newlist2 = list.Where(concat2).ToList();
  newlist1.ForEach(i => Console.WriteLine(i.ToString()));
  newlist2.ForEach(i => Console.WriteLine(i.ToString()));
  Console.ReadKey();
  #endregion
}

Conclusion:

The delegate of the Func class can pass in at least one input generic parameter (in, contravariant) and at most 16 input generic parameter (in, contravariant), and the output generic parameter (out, covariant) has and only one, which is the return value type of the method encapsulated by this delegate.

3. Predicate < T > entrust

Represents a method that defines a set of conditions and determines whether a specified object conforms to those conditions

Predicate is given below < T > Examples of delegation:


static void Main(string[] args)
{
  #region Predicate<T> Entrusted sample 
  // Requirement: Find an integer set list In more than 3 All of the elements that make up the new set, and print out the set elements 
  List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
  // Assign anonymous methods to  Predicate<T>  Delegate instance 
  Predicate<int> concat1 = delegate(int i) { return i > 3; };
  var newlist1 = list.FindAll(concat1);
  // will  lambda  Expression assigned to  Predicate<T>  Delegate instance 
  Predicate<int> concat2 = (c => c > 3);
  var newlist2 = list.FindAll(concat2);
  newlist1.ForEach(i => Console.WriteLine(i));
  newlist2.ForEach(i => Console.WriteLine(i));
       Console.ReadKey();
  #endregion
}

Conclusion:

Predicate < T > Delegate encapsulates a method, the method is introduced to a type parameter, this parameter is the type of object, in order of the type parameter is the inverter, and at the same time receive a parameter (this parameter is to be defined in accordance with the method of which I said conditions to compare the objects, the type of parameter is introduced into the type parameter type), the method always returns the value of bool type. true if the object meets the criteria defined in the method represented by this delegate; Otherwise false.

4. Comparison < T > entrust

Represents a way to compare two objects of the same type

Comparison is given below < T > Examples of delegation:


static void Main(string[] args)
{
  #region Comparison<T> Entrusted sample 
  // Requirement: Set integers list Print out all the elements in reverse order 
  List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
  // Assign anonymous methods to  Comparison<T>  Delegate instance 
  Comparison<int> concat1 = delegate(int i, int j) { return j - i; };
  // will  lambda  Expression assigned to  Comparison<T>  Delegate instance 
  Comparison<int> concat2 = (i, j) => j - i;
  list.Sort(concat1);
  list.ForEach(c => Console.WriteLine(c.ToString()));
  list.Sort(concat2);
  list.ForEach(c => Console.WriteLine(c.ToString()));
       Console.ReadKey();
  #endregion
}

Conclusion:

Comparison < T > Delegate encapsulates a method, the method is introduced to a type parameter, this parameter is the type of object, in order of the type parameter is inverter, accepts two parameters of the same type at the same time, these two parameters is to compare two objects, the type of parameter is introduced into the type parameter types), always return int type of value, namely a signed integer, indicates the relative x and y, shown in the following table.

含义
小于0 x 小于y 
0 x 等于y 
大于0 x 大于y 
Hopefully this article has helped you with your C# programming.

Related articles: