Monday, April 30, 2012

Delegates


The runtime supports constructs called delegates, which enable late-bound operations
such as method invocation and callback procedures. With delegates, a program can
dynamically call different methods at runtime. They are type safe, secure, managed
objects that always point to a valid object and cannot corrupt the memory of another
object. The closest equivalent of a delegate in other languages is a function pointer, but
whereas a function pointer can only reference static functions, a delegate can reference
both static and instance methods. Delegates are Marshal by Value Objects.
The members of a delegate are the members inherited from class System.Delegate.
A delegate defines the signature and return type of a method. The resulting delegate can
reference any method with a matching signature. Each instance of a delegate can forward
a call to one or more methods that take those parameters and return the same type. Once a
method has been assigned to a delegate, it is called when the delegate is invoked.


Example:
public delegate int calculation(int a,int b);
class mainclass
{
calculation calc_delegate;
public int add(int num1,int num2)
{
return num1 + num2;
}
static void Main()
{
int result;
mainclass obj = new mainclass();
obj.calc_delegate = new calculation(obj.add);
result = obj.calc_delegate(50,70);
}
}


Explanation:
Four steps are required to implement delegates viz.


• Defining Delegates
The foremost step is to define the delegate. The definition of the delegate specifies
the method signature, return type of the method, access modifier and the delegate
name. The method signature specifies the order and type of each argument.
The definition of a delegate is indicated by the usage of the delegate keyword. As
shown in the above code segment, the delegate name is calculation, it's access
modifier is public, it receives two integer arguments and returns an integer value.

• Creating Delegate Method Handler(s)
The next step is to define the method(s) that will be associated with the delegate.
In the above code segment, a method named add is defined. This method must have
same method signature as that of the delegate, as shown in the above code segment.

• Hooking up Delegates and Method Handlers
For a delegate method handler to be invoked, it must be assigned to a delegate object.
In the above code, the delegate object is calc_delegate and is hooked up to the
method handler add.

• Invoking the method through the Delegate

The last step is to invoke the methods that are associated with the delegate. A
delegate method handler is invoked by making a method call on the delegate itself.
This causes the method handler to invoke with the assigned input parameters as if
they were invoked directly by the program, as shown in the above code.



No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...