Theme-Logo
  •   .Net
    •   C Sharp(C#)
    •   Web API
    •   Micro Services
    •   ASP.Net
    • ASP.Net MVC
    • .Net Core
  •   Database
    • SQL Server
    • Oracle
    • PostgreSQL
  •   jQuery
    • jQuery Tutorials
    • jQuery Plugins
    • jQuery UI
    • More on jquery
  •   Tutorials
    • Microservices Tutorials
    • DotNet Core Tutorials
    • PostgreSql Tutorials

Delegate in C Sharp (C#)

In C#, delegates are often used to implement callbacks in event driven programming. For example, a delegate may be used to indicate which method should be called when the user clicks on some button. Delegates allow the programmer to notify several methods that an event has occurred

A delegate is a form of type-safe function pointer used by the Common Language Infrastructure (CLI). Delegates specify a method to call and optionally an object to call the method on. Delegates are used, among other things, to implement callbacks and event listeners. A delegate object encapsulates a reference to a method. The delegate object can then be passed to code that can call the referenced method, without having to know at compile time which method will be invoked.

A multicast delegate is a delegate that points to several methods. Multicast delegation is a mechanism that provides functionality to execute more than one method. There is a list of delegates maintained internally, and when the multicast delegate is invoked, the list of delegates is executed.

Declare a delegate in C#
                            delegate void Notifier(string sender);  // Normal method signature with the keyword delegate

                                Notifier greetMe;                       // Delegate variable

                                void HowAreYou(string sender) {
                                    Console.WriteLine("How are you, " + sender + '?');
                                }

                                greetMe = new Notifier(HowAreYou);
                        

A delegate variable calls the associated method and is called as follows:

                            greetMe("Anton");    // Calls HowAreYou("Anton") and prints "How are you, Anton?"
                        

Delegate variables are first-class objects of the form new DelegateType(obj.Method) and can be assigned to any matching method, or to the value null. They store a method and its receiver without any parameters:

                            new DelegateType(funnyObj.HowAreYou);
                        
Trending Post
What is Anonymous methods in C#.
Why Is It A Bad Idea To Throw Your Own Exceptions.
How's The DLL Hell Problem Solved In .net?
Interview Questions and Answers
Describe the Async method of C#?
Is it possible to serialise hashtables?
Explain the differences between “out” and “ref” parameters in C#?
How would you implement the Singleton design pattern in C#?
What Are The Ways To Deploy An Assembly?
What Are Advantages And Disadvantages Of Microsoft-provided Data Provider Classes In Ado.net?
About us

DotNet Palace is a community platform created and maintained by The articles are mainly focused on Microsoft stack technologies like C#, ASP.Net, MVC, .Net Core, SQL Server and Oracle, PostgreSQL, SQLite etc. To improve the site's content you can send your valuable suggestions at info.dotnetpalace@gmail.com

Quick links
  • SOLID Principles
  • Questions
  • OOPS Principles
  • DotNet Palace Tutorials
  • Privacy Policy
  • Terms and Condition