Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Sunday, May 28, 2017

Delegate in C#


Delegate is a type safe function pointer supporting encapsulation.Delegate is a keyword that after compilation,behaves as a class.

Delegate is an keyword that can refer to a method. Therefore, when you create a delegate, you are creating an object that can hold a reference to a method. Furthermore, the method can be called through this reference. In other words, a delegate can invoke the method to which it refers.
A key point to understand is that a delegate can be used to call any method that agrees with its signature and return type.

A delegate declaration defines a type that encapsulates a method with a particular set of arguments and return type. For static methods, a delegate object encapsulates the method to be called. For instance methods, a delegate object encapsulates both an instance and a method on the instance. If you have a delegate object and an appropriate set of arguments, you can invoke the delegate with the arguments. An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.

A delegate type maintains three important pieces of information : 
The name of the method on which it make calls. 
Any argument (if any) of this method. 
The return value (if any) of this method.

To interact between .NET and Operating system , Delegate is called.We achieve pure encapsulation by delegate.
from object of class,we can see all function but from object of delegate we cannot see all function.we can see specified function,delegate is used for proper hiding of information.

Syntax of a delegate
Step 1: Declaration
Delegate is getting declared here.
        Modifer  delegate  return_type  delegate_name ( [Parameter....])
Step 2: Instantiation
Object of delegate is getting created as passing method as argument
        Delegate_name delg_object_name = new Delegate_name( method_name);
Here method_name signature  must be same as of signature of delegate.
Step 3: Invocation
Delegate is getting called here.
         Delg_object_name([parameter....]);
Example
Step 1- Declaration

    public delegate int MyDelegates(int x,int y);


Step 2- This class contains methods that MyDelegate will point to.
public class MyClass
    {
        public static int Add(int x, int y)
        {
            return x + y;
        }
        public static int Multiply(int x, int y)
        {
            return x * y;
        }
    }

Step 3 -Create an Instance of MyDelegate

class Program
    {
        static void Main(string[] args)
        {
            MyDelegates MyDelegates = new MyDelegates(MyClass.Add);
            int addResult = MyDelegates(5, 5);
            Console.WriteLine("5 + 5 = {0}\n", addResult);

            MyDelegates MyDelegates1 = new MyDelegates(MyClass.Multiply);
            int addResult1 = MyDelegates1(5, 5);
            Console.WriteLine("5 * 5 = {0}\n", addResult1);
            Console.ReadLine();
        }
    }


Whole Program is look like---
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace @delegate
{
    public delegate int MyDelegates(int x,int y);
    class Program
    {
        static void Main(string[] args)
        {
            MyDelegates MyDelegates = new MyDelegates(MyClass.Add);
            int addResult = MyDelegates(5, 5);
            Console.WriteLine("5 + 5 = {0}\n", addResult);

            MyDelegates MyDelegates1 = new MyDelegates(MyClass.Multiply);
            int addResult1 = MyDelegates1(5, 5);
            Console.WriteLine("5 * 5 = {0}\n", addResult1);
            Console.ReadLine();
        }
    }
    public class MyClass
    {
        public static int Add(int x, int y)
        {
            return x + y;
        }
        public static int Multiply(int x, int y)
        {
            return x * y;
        }
    }

}

Output-


You may also like

No comments :

Post a Comment