If I searched I will definitely get examples of showing what is a Delegate and Action. I have read the basic books giving examples of delegate.
But what I want to know is their use in the real world. Please do not give Hello World example or the Animal class they are far too basic
For e.g. :
A delegate specifies a TYPE (such as a class , or an interface does), whereas an event is just a kind of MEMBER (such as fields, properties, etc). And, just like any other kind of member an event also has a type. Yet, in the case of an event, the type of the event must be specified by a delegate.
Delegates Overview Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a single event. Methods don't have to match the delegate type exactly.
A delegate is an object-oriented, managed, secure and type-safe function pointer in the . NET framework. A delegate signature includes its name, return type and arguments passed to it. Rather than passing data, a delegate passes a method to another method.
Action
is a Delegate
. It is defined like this:
public delegate void Action();
You could create your own delegate types similarly to how you would create abstract methods; you write the signature but no implementation. You then create instances of these delegates by taking the reference of a method.
class Program { public static void FooMethod() { Console.WriteLine("Called foo"); } static void Main() { Action foo = FooMethod; // foo now references FooMethod() foo(); // outputs "Called foo" } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With