Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • HOW TO USE DELEGATE IN C# and ASP.NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 440
    Comment on it

    Delegate is a type which holds the method(s) reference in an object. It is also referred to as a type safe function pointer. Delegates concept will match with function pointer concept of c++ language.

    We use delegate keyword when we need to create delegate methods. Delegate methods should match with the signature of the methods that are defined under the classes.Delegate method should have same return type and same parameters,if the signature mismatch then program will show compile error.

    Delegate is of namespace level. it contains all the classes under the namespace and able to access all the functions defined under the classes.Delegate is more secure than function pointer because the function is passed in the delegate with the help of class object as defined in the below example and then delegate is invoked. Because the user don't have idea about the address of delegate so he is unable to access the function too. That's why it is called type safe function pointer.

    Declaration:

    public delegate type_of_delegate delegate_name()
    

    Example for above syntax:

    public delegate int mydelegate(int delvar1,int delvar2)
    

    Types of delegate

    1. Singlecast delegate
    2. Multicast delegate

    Singlecast delegate:

    Single cast delegate means which hold address of single method like as explained in below example.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace helloapp
    {
    public delegate int mydel(int a,int b);
    class hello
    {
    public int add(int a,int b)
    {  int res1=a+b;
    return res1;
    }
    public int sub(int a,int b)
    {  int res2=a-b;
    return res2;
    }
    }
    static void main(string[] args)
    {
      hello ob=new hello();
    mydel d1=new mydel(ob.add);
    int res1=d1(2,3);
    console.writeline(result:{0}, res1);
    mydel d2=new mydel(ob.sub);
    int res2=d2(5,3);
    console.writeline(result:{0}, res2);
    }
    }
    

    OUTPUT:

    res1:5
    res2:2

    Multicaste delegate:

    Multi cast delegate is used to hold address of multiple methods in single delegate. To hold multiple addresses with delegate you have to use overloaded += operator and if you want remove addresses from delegate the you need to use overloaded operator -=

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace helloapp
    {
    public delegate int mydel(int a,int b);
    class hello
    {
    public int add(int a,int b)
    {  
    int res=a+b;
    return res;
    }
    public int sub(int a,int b)
    {  int res=a-b;
    return res;
    }
    public int mul(int a,int b)
    {  int res=a*b;
    return res;
    }
    public int div(int a,int b)
    {  int res=a/b;
    return res;
    }
    public int mod(int a,int b)
    {  int res=a%b;
    return res;
    }
    }
    static void main(string[] args)
    {
     hello ob=new hello();
     mydel d1=hello.add;
     d1+=hello.sub;
    d1+=hello.mul;
    d1+=hello.div;
    d1+=hello.mod;
    int res=d1(10,5);
    Console.WriteLine ("Result :"+res);
    
    Console.Readline();
    }
    }
    

    OUTPUT:

    res: 15 5 50 2 0

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: