6
Reply

what is delegate

reeta

reeta

15y
4.9k
0
Reply

    Delegate is a type safe Functional Pointer. it hold method or methods reference in an object.effective use of delegate to improve performance of application.

    Declaration

    public delegate type_of_delegate delegate_name(var_type var1,var_type var2)

    Note :-

    1. you can use delegate with parameters or without parameters
    2. you should follow the same syntax as in the method

    Sample program using Delegate

    //delegate Declaration

    public delegate double delegate_prod(int a,int b)

    Class Class1

    {

       //method with the same parameters and same signature

       static double fnprodvalue(int val1,int val2)

        {

            return var1*var2;

        }

       static void main(string[] args)

      {

          //creating delegate instance

          delegate_prod objDelegate =new delgate_prod(fnprodvalue);

         double res = objDelegate(5,4);

          console writeline("Result :",  + res);

          console.readline():

       }

    }

    Delegates are two types

    single caste -single caste delegate refers only one function address after creating the instance variable

    multi-caste delegate- multi-caste refers to multi-single caste delegate features

    Delegate is a Functional Pointer and it acts as a reference type too. Depending on function prototype we have to declare the delegates. It takes arguments as well as return types also. they are two types single caste and multi-caste delegate. single caste delegate refers only one function address after creating the instance variable.and it holds only one method address. were as multi-caste refers to multi-single caste delegate features. i.e we can access multiple features from the class.

    A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

    Examples:
    using System;

    namespace Akadia.BasicDelegate
    {
        // Declaration
        public delegate void SimpleDelegate();

        class TestDelegate
        {
            public static void MyFunc()
            {
                Console.WriteLine("I was called by delegate ...");
            }

            public static void Main()
            {
                // Instantiation
                SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

                // Invocation
                simpleDelegate();
            }
        }
    }

     

    One important features of delegate is It can be invoked asynchronously. That method would be executed in different thread. Even you can receive return type.

    Delegates works as a functional pointers.Delegates are pure safe they hide the actual

    information.Delegate may point aome function or more than one function.Delegates

    may return a value.Delegates are declared class level,structurelevel,module level,

    global level.

     

    Delegate

    A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value,

    Visit

    www.dotnetask.blog.co.in