delegates in c# programming

Delegates in c# programming-: A delegate is a type used to reference a method. This allows methods to be assigned to variables and passed as arguments. The delegate’s declaration specifies the method signature to which objects of the delegate type can refer. Delegates are by convention named with each word initially capitalized, followed by Delegate at the end of the name.
delegate void MyDelegate(string s);
A method that matches the delegate’s signature can be assigned to a delegate object of this type.
class MyClass
{
static void Print(string t)
{
System.Console.WriteLine(t);
}
static void Main()
{
MyDelegate d = Print;
}
}
This delegate object will behave as if it were the method itself, regardless of whether it refers to a static or an instance method. A method call on the object will be forwarded by the delegate to the method, and any return value will be passed back through the delegate.

MyDelegate d = Print;
d(“Hello”); // “Hello”

The syntax used here to instantiate the delegate is actually a simplified notation that was introduced in C# 2.0. The backwards compatible way to instantiate a delegate is to use the regular reference type initialization syntax.
MyDelegate d = new MyDelegate(Print);

Delegate Declaration -: Delegate types in C# are name equivalent, not structurally equivalent. Specifically, two different delegate types that have the same parameter lists and return type are considered different delegate types.A delegate-declaration is a type-declaration that declares a new delegate type.

delegate in c#

Delegate Compatibility-:A method or delegate M is compatible with a delegate type D if all of the following are true

  •  D and M have the same number of parameters, and each parameter in D has the same ref or out modifiers as the corresponding parameter in M.
  • For each value parameter (a parameter with no ref or out modifier), an identity conversion  or implicit reference conversion  exists from the parameter type in D to the corresponding parameter type in M.
  • For each ref or out parameter, the parameter type in D is the same as the parameter type in M.
  • An identity or implicit reference conversion exists from the return type of M to the return type of D.

Delegate Instantiation-:An instance of a delegate is created by a delegate-creation-expression  or a conversion to a delegate type. The newly created delegate instance then refers to either

  •  The static method referenced in the delegate-creation-expression, or
  • The target object (which cannot be null) and instance method referenced in the delegate-creation-expression, or
  • Another delegate. example of

 

delegate instanation

 

Delegate Invocation-:  C# provides special syntax for invoking a delegate. When a non-null delegate instance whose invocation list contains one entry is invoked, it invokes the one method with the same arguments it was given, and returns the same value as the referred to method.

It is possible for a delegate object to refer to more than one method. Such an object is known as a multicast delegate and the methods it refers to are contained in a so-called invocation list. To add another method to the delegate’s invocation list, either the addition operator or the addition assignment operator can be used.
static void Hi() { System.Console.Write(“Hi”); }
static void Bye() { System.Console.Write(“Bye”); }
// …
MyDelegate del = Hi;
del = del + Hi;
del += Bye;
Similarly, to remove a method from the invocation list, the subtraction or subtraction assignment operators are used.
del -= Hi;
When calling a multicast delegate object, all methods in its invocation list will be invoked with the same arguments in the order that they were added to the list.
del(); // “HiBye”
If the delegate returns a value, only the value of the last invoked method will be returned. Likewise, if the delegate has an out parameter, its final value will be the value assigned by the last method.

If an exception occurs during the invocation of such a delegate, and that exception is not caught within the method that was invoked, the search for an exception catch clause continues in the method that called the delegate, as if that method had directly called the method to which that delegate referred.

Invocation of a delegate instance whose invocation list contains multiple entries proceeds by invoking each of the methods in the invocation list, synchronously, in order. Each method so called is passed the same set of arguments as was given to the delegate instance.

The following example shows how to instantiate, combine, remove, and invoke delegates

 

 

delegate example

Output of this  programme

C.M1: 20
C.M2: 20
C.M1: 20
C.M1: 30
C.M2: 30
C.M1: 30
C.M3: 30
C.M1: 40
C.M2: 40
C.M3: 40
C.M1: 50
C.M2: 50
C.M1: 60
C.M1: 60

this is the example of delegates

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *