SoFunction
Updated on 2025-03-07

Detailed explanation of the common system built-in delegation usage in C#

Generally speaking, C# system has built some commonly used delegates for us in the common language runtime (CLR) environment, including the delegate of Action class, the delegate of Func class, the Predicate<T> delegate, the Comparison<T> delegate, and so on. The namespaces of the above delegations are System, and the assembly they belong to are all. Today, this article will talk about the usage methods of these delegations.

Just like we have defined ourselves, to implement certain functions, we can directly utilize the system's built-in delegates and instantiate them without having to explicitly define a new delegate and assign a named method to that delegate. like:

public static void Test()
{
  ("Just For Test");
}
static void Main(string[] args)
{
  Action a = new Action(Test); //Directly instantiate an Action delegation without defining a new delegation yourself  a();
}

As long as you understand what the built-in delegation of the system is for, what parameters are passed, and what values ​​are returned, you can call it by yourself by imitating the above examples and will not be repeated. Below is my summary of these four types of delegations, and an example of combining anonymous methods and Lambda expressions. The results of the two methods are the same. You can learn from one example and apply them flexibly.

1. Delegation of Action

Delegate Encapsulate a method that has no parameters and does not return a value

<T>Delegate Encapsulates a method that has only one parameter and does not return a value

<T1,T2>Delegation Encapsulates a method that has two parameters and does not return a value

…… ……

<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16>Delegation Encapsulate a method that has 16 parameters and does not return a value

Let’s take Action<T> delegation as an example to demonstrate how to use the delegation of the Action class. This delegation is just a difference in the number of parameters.

static void Main(string[] args)
{
#region Action<T>Delegation Example  //Requirements: Print out the elements of the integer collection list  List&lt;int&gt; list = new List&lt;int&gt;() { 1, 2, 3, 4, 5 };
  // Assign anonymous methods to the Action<T> delegate instance  Action&lt;int&gt; concat1 = delegate(int i) { (i); };
  (concat1);
  // Assign the lambda expression to the Action<T> delegate instance  Action&lt;int&gt; concat2 = (i =&gt; (i));
  (concat2);
  ();
#endregion 
}

Summarize:

The delegate of the Action class can pass at least 0 parameters and up to 16 parameters. The parameter types are inverse and no value is returned.

2. Func class delegation

(TResult) Delegate Encapsulation encapsulates a method that does not have a parameter but returns the type value specified by the TResult parameter

(T,TResult) Delegation Encapsulate a method that has a parameter and returns the type value specified by the TResult parameter

(T1,T2,TResult) Delegation Encapsulate a method that has two parameters and returns the type value specified by the TResult parameter

…… ……

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

Let’s take Func<T,TResult> delegation as an example to demonstrate how to use Func class delegation. This class delegation is just a difference in the number of parameters.

static void Main(string[] args)
{
  #region Func<T,TResult>Delegation Example  //Requirements: Find a new set composed of all elements greater than 3 in the integer set list, and print out the set elements  List&lt;int&gt; list = new List&lt;int&gt;() { 1, 2, 3, 4, 5 };
  // Assign anonymous methods to Func<T,TResult> delegate instance  Func&lt;int, bool&gt; concat1 = delegate(int i) { return i &gt; 3; };
  var newlist1 = (concat1).ToList();
  // Assign the Lambda expression to the Func<T,TResult> delegate instance  Func&lt;int, bool&gt; concat2 = i =&gt; i &gt; 3;
  var newlist2 = (concat2).ToList();
  (i =&gt; (()));
  (i =&gt; (()));
  ();
  #endregion
}

Summarize:

The Func class delegate can pass at least 1 input generic parameters (in, inverter) and up to 16 input generic parameters (in, inverter) and there are only one output generic parameters (out, covariance). This type is the return value type of the method encapsulated by this delegate.

3. Predicate<T>Trust

Represents a method that defines a set of conditions and determines whether the specified object meets these conditions

The following is an example of Predicate<T> delegation:

static void Main(string[] args)
{
  #region Predicate<T>Delegation Example  //Requirements: Find a new set composed of all elements greater than 3 in the integer set list, and print out the set elements  List&lt;int&gt; list = new List&lt;int&gt;() { 1, 2, 3, 4, 5 };
  // Assign anonymous methods to the Predicate<T> delegate instance  Predicate&lt;int&gt; concat1 = delegate(int i) { return i &gt; 3; };
  var newlist1 = (concat1);
  // Assign the lambda expression to the Predicate<T> delegate instance  Predicate&lt;int&gt; concat2 = (c =&gt; c &gt; 3);
  var newlist2 = (concat2);
  (i =&gt; (i));
  (i =&gt; (i));
 ();
  #endregion
}

Summarize:

Predicate<T> delegate encapsulates a method, which passes in a type parameter. This parameter refers to the type of the object to be compared. This type parameter is an inverter and receives a parameter at the same time (this parameter is the object to be compared according to the conditions defined in the method represented by this delegate. The type of the parameter is the type of the type parameter passed in). This method always returns the value of the bool type. true if the object meets the conditions defined in the method represented by this delegate; false otherwise.

4. Comparison<T>Command

Methods for comparing two objects of the same type

Here is an example of Comparison<T> delegation:

static void Main(string[] args)
{
  #region Comparison<T>Delegation Example  //Requirements: Print out all elements in the integer collection list in reverse order  List&lt;int&gt; list = new List&lt;int&gt;() { 1, 2, 3, 4, 5 };
  // Assign anonymous methods to Comparison<T> delegate instance  Comparison&lt;int&gt; concat1 = delegate(int i, int j) { return j - i; };
  // Assign the lambda expression to the Comparison<T> delegate instance  Comparison&lt;int&gt; concat2 = (i, j) =&gt; j - i;
  (concat1);
  (c =&gt; (()));
  (concat2);
  (c =&gt; (()));
 ();
  #endregion
}

Summarize:

Comparison<T> delegates to encapsulate a method, which passes in a type parameter. This parameter refers to the type of the object to be compared. This type parameter is an inverter and receives two parameters of the same type at the same time (these two parameters are the two objects to be compared, and the type of the parameter is the type of the type parameter passed in). It always returns the value of type int, that is, a signed integer indicating the relative value of x and y, as shown in the following table.

value meaning
Less than 0 x Less thany 
0 x equaly 
Greater than 0 x Greater thany 
I hope this article will be helpful to everyone's C# programming.