Difference between delegate, delegate, func, action, predicate

Time:2021-11-28

C# common delegates include delegate, action, func and predict. What are the differences and connections between them?

1. Func, action, predicate differences

definition

Viewing the source code, you can find:

  1. Action, func and predicate are generic delegates defined by the system using delegate.

  2. They have different parameters and return values.

  3. Defined using in, outInversion and covariance

public delegate void Action<in T>(T obj);
public delegate TResult Func<in T, out TResult>(T arg);
public delegate bool Predicate<in T>(T obj);

According to the source code:

  1. We can use delegate instead of action, func and predicate.

  2. We can also define our own delegates in a similar way.

You can also judge the general usage scenario according to the name of the delegate:

  1. Action represents an action and an action is executed, so it does not have a return value.

  2. Func represents an ordinary function with parameters and return values.

  3. Predicate assertion is often used to judge whether the data meets the requirements.

Difference between parameter and return value:

  1. Func has at least 0 parameters and at most 16 parameters. It must have a return value and cannot be void.

       public int Test<T1,T2>(Func<T1,T2,int>func,T1 a,T2 b)
       {
           return func(a, b);
       }
    
  2. Action has at least 0 parameters and at most 16 parameters, with no return value.

       public void Test<T>(Action<T> action,T p)
       {
           action(p);
       }
    
  3. Predicate has only one parameter, and the return value must be bool.

       public bool Test<T>(Predicate<T> predicate,T p)
       {
           return predicate(p);
       }
    

2. Differences among delegate, delegate and multicastdelegate

Hierarchy of delegated inheritance

System.Object  
=> System.Delegate    
==> System.MulticastDelegate
===>UsedelegateKeyword custom delegate

Inheritance hierarchy source

First 3 floors(Object,Delegate,MultiDelegate)From document.
delegateInheritance is derived from the intermediate language generated after code compilation.

useildasm.exeAs you can see, the compiler willdelegateCompile as inheritanceMulticastDelegateClass of

public delegate void BinaryOp(int x);

Difference between delegate, delegate, func, action, predicate

difference

Delegate

DelegateIs the base class of a delegate. Some basic methods of delegation are defined.

MulticastDelegate

MulticastDelegateinheritDelegate, and some extensions are made.

delegate

We can’t inherit directlyDelegateandMulticastDelegate。 Only systems and compilers can inherit them.
We need to usedelegateKeyword, and the compiler will automatically compile the delegate as an inheritanceMulticastDelegateAnd addBeginInvoke,EndInvoke,InvokeAnd other methods. For example, the figure aboveFigure 1

summary

  1. Action,Func,PredicateAre used by the systemdelegateDefined delegate.

  2. usedelegateA defined delegate is actually an inheritanceMulticastDelegateClass (which is why using delegates firstnewReasons for:BinaryOp a = new BinaryOp(o=>{});)。

This article explains the differences between several common delegates. If there are errors or omissions, please point them out in the comments. thank you.

Reference article:

  1. Introduction to c# entrustment

  2. Delegation (c# Programming Guide)

  3. Delegate Class

  4. System.delegate and delegate keywords

Recommended Today

On the mutation mechanism of Clickhouse (with source code analysis)

Recently studied a bit of CH code.I found an interesting word, mutation.The word Google has the meaning of mutation, but more relevant articles translate this as “revision”. The previous article analyzed background_ pool_ Size parameter.This parameter is related to the background asynchronous worker pool merge.The asynchronous merge and mutation work in Clickhouse kernel is completed […]