A summary of the basic usage of delegates in c sharp

  • 2020-04-02 01:41:58
  • OfStack

Principle:

1. A delegate is essentially a pointer, a function pointer, to get the first address of the function;

C# 's delegate adds security, which is reflected in the type detection of the function instruction block referenced by the function pointer, such as return value, parameter type, parameter number

And function pointer in C are assignment (in C #, is the time when the delegate instantiation, because C # in the background to entrust processing into a class, encapsulates ha) was given whether the value of the type of a variety of conditions, the return value, parameter type, number of parameters) do not check, guarantee is given by the user, compile the C # prompt out

2, The process of delegate instantiation, that is, the process of delegate object construction, from the bottom, is to assign the memory address of an existing function code instruction block (static function, the instance of the non-static member function) to this delegate; Constructor, the assignment process is entrusted to do, so, there must be a constructor argument, entrusted by the processing, the parameters in the background system is to satisfy a function pointer type checking, to check the type (the return value, parameter type, number of parameters) are defined in the statement given delegate.

3. Two methods of delegate instantiation:

1) A A = new A (static method name/instance. Method name);

2) A A = static method name/instance;

Two methods of delegate invocation:

1) Anderson nvoke ()

2) a ()

Used to construct the entrusted method can be static, it can also be instantiated object's member functions, nature is the code instruction has been compiled, allocate memory, parameter passed to the entrusted (essence is the command data first address) is a valid sense, from this Angle to understand, natural static methods, instance methods can be used to construct and establish a delegate instance


Related articles: