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();
        }
    }
}

10 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. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command .

    digital marketing company in india

    ReplyDelete
  5. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    Software Testing Training Institute in Chennai

    ReplyDelete
  6. Great post! I am see the programming coding and step by step execute the outputs.I am gather this coding more information. It's helpful for me my friend. Also great blog here with all of the valuable information you have.
    Germany Education Consultants in Chennai

    ReplyDelete
  7. Very nice post here and thanks for latest smartphone applications it .I always like and such a super colors of phone for these post.Excellent and very cool idea and great models and different kinds of the more information's.

    Fresher Jobs in Mumbai
    Fresher Jobs in Pune
    Fresher Jobs in Noida
    Fresher Jobs in Hyderabad

    ReplyDelete

  8. Wonderful blog.. Thanks for sharing informative Post. Its very useful to me.

    Installment loans
    Payday loans
    Title loans

    ReplyDelete
  9. I simply want to tell you that I’m all new to blogs and truly liked you’re blog site. Very likely I’m likely to bookmark your site .You surely come with remarkable articles. Cheers for sharing your website page.
    Digital Marketing Company in Chennai

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...