Personal notes on delegation and events of Unity and C#

Basic custom delegate form:

Public   delegate  void  Delegate(){

}

A delegate is a reference type of class. Similar to cpp function pointer. The basic use of delegates is to quickly call certain methods indirectly.

The C#system library comes with a delegate:

Action delegate has no return value (void)

Func delegates must have a return value.

Delegates are often used to pass in a method as a parameter, and at the same time indirectly call other methods in the delegate. Enables fast coupling of multiple methods.

shortcoming:

The multicasting of delegates will cause memory leaks: the delegate will refer to a method. If the method is an instance method (non-static method), then this method must belong to an object. If a delegate refers to this method, then this object must exist In memory, even if there are no other reference variables to refer to this object, the memory of this object cannot be released, because once released, the memory can no longer indirectly call the method of the object. memory leak

Multicast delegation also has certain difficulties in maintenance, and it is easy to cause all methods to be reset, and the integrity is reduced. (Use events as wrappers for delegate type fields.)

Nested calls are less readable

.NET stipulates that the naming format of the delegate for declaring an event adopts the event name plus the suffix EventerHandle. Event names should be suffixed with EventArgs, and custom events should inherit from EventArgs.

Events are essentially wrappers around delegated fields.

Public EventHandler onorder; this is a delegate type field.

Public event EventHandler onorder ; This is an event, which can only be accessed with -= or +=, adding subscription and deletion of event handlers. The abbreviated declaration format is easily misleading. In fact, the event is not a field of the delegate type or a special delegate type, but a wrapper for the delegate.

PS: It can be used to judge whether the event is empty! = (syntactic sugar).

Full declaration format:

Private EventHandler orderEventHandler;

Pubic event EventHandler Onorder{

  add{

          orderEventHandler+=value;

   }

   remove {

           orderEventHandler-=value;

    }

}

Events are used to castrate delegate instances. Events can only add and remove event handlers, but cannot be assigned values, and events can only be +=, -=, and cannot be =, and events cannot be triggered externally.

In actual projects, we often use events to reduce the coupling between codes, and at the same time avoid repetitive writing of multiple more general methods to reduce workload.

Postscript: It can be regarded as a personal feeling about the recent Tangshan incident. I hope that everyone can take a deeper look at factors such as gender opposition, women's rights, sexual harassment, underworld, and violent fights, and have your own thinking. I hope that the victims in this incident can be taken care of by everyone, and that the lawbreakers can be punished. At the same time, I always hope that the majority of netizens can be more rational.

Guess you like

Origin blog.csdn.net/qq_62440805/article/details/125242794