blogspot visit counter

Friday, 23 May 2014

Delegate in c#

Delegate in c#
Delegate :- Delegates in C# are objects which points towards a function which matches its signature. Delegates are reference type used to encapsulate a method with a specific signature. Delegates are similar to function pointers in C++; however, delegates are type safe and secure.
Features of delegate

    A delegate represents a class.
    delegate is type-safe.
    We can use delegates both for static and instance methods
    We can combine multiple delegates into a single delegate.
    Delegates are often used in event-based programming, such as




publish/subscribe.

    We can use delegates in asynchronous-style programming.
    We can define delegates inside or outside of classes.

Types of delegates

1) Singlecast delegates.
2) Multiplecast delegates.

Singlecast delegate : - Singlecast delegate point to single method at a time. In this the delegate is assigned to a single method at a time. They are derived from System.Delegate class.
Examples:
   
namespace DelegatesSample
{
   
   public delegate int Delegateclass(int a,int b);
    class Program
    {
        static int fun(int x, int y)
        {
            return x + y;
        }
        static void Main(string[] args)
        {
            Delegateclass delobj = new Delegateclass(fun);
            Console.WriteLine("Create in values");
            int v1 = Int32.Parse(Console.ReadLine());
            int v2 = Int32.Parse(Console.ReadLine());
            int res = delobj(v1,v2 );
            Console.WriteLine("Results" + res);
            Console.ReadLine();
        }
    }
}

Multicast Delegate :- When a delegate is wrapped with more than one method that is known as a multicast delegate.In C#, delegates are multicast, which means that they can point to more than one function at a time. They are derived from System.MulticastDelegate class.
Example:
   
namespace MuticastDelegates
{
    public delegate int Calculator(int a,int b);
    class MultiDelegates
    {
        Calculator calcul1;
        Calculator calcul2;
        public int Add(int x, int y)
        {
            return x + y;
        }
        public int sum(int n, int m)
        {
            return n - m;
        }
        static void Main(string[] args)
        {
            MultiDelegates mydel = new MultiDelegates();
            mydel.calcul1 = new Calculator(mydel.Add);

            mydel.calcul2 = new Calculator(mydel.sum);
            mydel.calcul2 = new Calculator(mydel.Add);
            int results = mydel.calcul1(12, 12);
            Console.WriteLine("Results" + results);
            results= mydel.calcul2(24, 12);
            Console.WriteLine("Results" + results);
            results = mydel.calcul2(10, 5);
            Console.WriteLine("Results" + results);
            Console.ReadLine();
        }
    }
}

14 comments:

  1. http://allinterviewquestionsandanswerspdf.blogspot.in/2016/06/top-69-vb-net-interview-questions-and.html

    ReplyDelete

  2. the blog is very useful, interesting and informative. thank you for sharing the blog with us. keep on updating.
    Linux Training in Chennai

    ReplyDelete
  3. This is really a very great blog. the information present in this blog will be very useful for us. thank you for sharing with us.
    Email Marketing Chennai

    ReplyDelete
  4. Can’t find a reliable service to order your paper from. Check our freshessays reviews on Scamfighter. Only real reviews based on personal experience and clients’ experience.

    ReplyDelete
  5. Thanks for sharing such an informative post with us.
    MYOB Support

    ReplyDelete
  6. Waktu ini, kalian semua jelas sudah biasa tergugah dengan sifat dasar game ini & tentu bertanya-tanya bagaimana jalan tampil selang poker. Tapi, bagi semua orang yang target memainkan game itu
    asikqq
    http://dewaqqq.club/
    http://sumoqq.today/
    interqq
    pionpoker
    bandar ceme terpercaya
    betgratis
    paito warna terlengkap
    syair sgp

    ReplyDelete
  7. I'm a web developer and as many developers I am an intovert. It has always been a huge problem to approach a girl and talk to her. Online dating seemed to be a solution to my problem but there are so many different sites. I read every review very carefully and found many positive reviews on charmerly. That's why I created my profile here and started chatting to pretty girls.

    ReplyDelete
  8. You may need to experience Crypto Bitcoin. I also sent an e-mail with regard to Crypto Bitcoin.

    Where can mere mortals nab outstanding Crypto Bitcoin catalogs? If you need to know what I'm talking about, keep reading this installment.

    Bitcoin Era


    http://wealthcode.over-blog.com/bitcoin-era-erfahrung


    https://www.facebook.com/bitcoinera.official/

    ReplyDelete
  9. Please more of these great articles. I like the way you convey ideas in a simple way that’s easy to understand. Thanks!
    AWS Training in Hyderabad

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...