Labels

slider

Recent

Navigation

C#: Learn use of Delegates in C# programming

How to use Delegates for event handling in C# programming! Free learning .Net Tutorials for the beginners!

Learn use of Delegates in C# programming

Hope you must have gone through my previous articles Class members, Inheritance & Interface, and Generics.  Now, we’ll discuss on Delegates. A delegate defines a signature method and can create a reference to any method with a well-matched signature. You can call the method only through the delegate. Delegates help pass methods as point a of view to other methods for the purpose of signature validation between two methods.

C#: Learn use of Delegates in C# programming

Please note that Event handlers are similar to methods that are called through delegates. The Event handling task is very much important for a programming aspect. Hence, delegates are broadly used in event handling.

Codes to create a delegate:

public delegate void TCDelegate(string str);

Codes to create a reference to a method, which perfectly matches the signature mentioned by the delegate:

class SampleClass
{
    // Method that matches the TCDelegate signature.
    public static void TCMethod(string message)
    {
        // Add code here.
    }

    // Method that instantiates the delegate.
    void TCDelegate()
    {
        TCDelegate sd = tcMethod;
        sd("Sample string");
    }
} 
Overall, Delegates are the vital signature methods for the validation of signatures between two methods. The above code must help you in using delegate in an error-free manner.
Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

0 comments: