✕
Forbes India Leader Recognition
e.preventDefault(); // Prevent form submission input.blur(); // remove focus input.value = ""; // optional: clear the input

Delegates and Events in C#

Delegates and Events in C#

Featured Image

What is Delegate

  • It is the same as function pointer in C and C++.
  • It is type-safe as it gives compile-time error when referencing any method. 
  • It holds the reference of a method and then calls that method for execution whenever the delegate is invoked.

Delegate Signature:

public delegate int DelegateMethod(int x, int y);  

Function with the same delegate’s signature that contains return type and the parameters, can be assigned to that delegate.

Why to use Delegate

  • We as developers often need to pass a function as a parameter to another function. For this purpose, we create and use them.
  • It is a class that encapsulates a method signature.

Types

  • SingleCast

    • This type of delegate holds the reference of only a single method and they are derived from System.Delegate class.

image-1.png

  • MultiCast

    • This type of delegate holds the reference of more than one method and they are derived from System.MulticastDelegate class.
    • The + and += operators are used to combine delegate instances.
    • The – and -= operators are used to remove delegate instances.

image-2.png

To get rid of all above steps 

  1. Creating a delegate
  2. Creating a Method to perform some action
  3. Referencing the method to that delegate.
  4. We can skip the second step and third step by using anonymous functions or using lambda functions.

AnonymousMethods and LambdaExpression

image-3.png

Generic Delegates

  • They were introduced as part of .NET Framework 3.5 which doesn’t require defining the delegate instance in order to invoke the methods

 Types

  • Func

    • It takes 0 or more input parameters upto 16 and returns a single value.
    • It does not allow ref and out parameters.
    • It can be used with anonymous methods and lambda expressions.
    • Syntax: Func<int, int, int> Sum = reference_method_name;
    • reference_method_name can be method name to add reference or it can be anonymous methods or lambda expressions.
  •  Action

    • It  takes 0 or more input parameters upto 16 and does not return any value.
    • It can be used with anonymous methods and lambda expressions.
    • Syntax: Action<int> display =  reference_method_name;
    • reference_method_name can be method name to add reference or it can be anonymous methods or lambda expressions.
  • Predicate

    • It takes one or more input parameters and returns bool value.
    • It is mainly used for checking conditions.
    • Syntax: Predicate<string> isLower = reference_method_name;
    • reference_method_name can be method name to add reference or it can be anonymous methods or lambda expressions.

Events:

An event is some action that is going to happen. 

For example, Tudip organizes an event for the employees, to make them aware about the features of new or existing products. 

Tudip notifies the employees about the event by email or other options.

In this scenario, Tudip is a publisher who organizes (raises) the event and notifies the employees about that and employees are the subscribers of that event and to attend the event is likely to handle the event.

Events are the important part in user interfaces and programming notifications. 

Events and the delegate work together. It is a reference to the delegate that means when an event is raised, the linked delegate gets called. 

When to use Events

When the code from different classes needs communication between them that means when something happens in one class or at some part of code and another part of the code needs a notification, events are used. event and delegate works hand-in-hand.

Syntax:

public event DelegateHandler delegateEvent;

Here the DelegateHandler is the delegate and is like  public delegate void DelegateHandler(); 

And we can linked the method with the event as follows:

  delegateEvent += new DelegateHandler(method_name); 

EventHandler Syntax:

public event EventHandler delegateEvent;

Events without delegates?

No, events use delegates internally so it is required but can be skipped if we use EventHandler with the event keyword while creating an event that does not need to create a delegate for that event as the EventHandler uses the delegate internally.

Event Handlers return void and take two input parameters.

The first parameter of Event Handler is source of event that means the sender and the second parameter of Event Handler is the object derived from EventArgs.

 

Related Posts

Latest Posts

  • All Posts
  • AI Powered Knowledge
  • ai/ml
  • CEO India Magazine
  • CMMI level 5 Certification
  • e-learning
  • Fintech
  • gaming
  • Generative AI
  • healthcare
  • manufacturing
  • News
  • OTT
  • Portfolio
  • supply chain
  • travel and hospitality
  • Tudip's AI Hackathon
  • Voxlearn Enterprises
    •   Back
    • Android
    • iOS
    • Java
    • PHP
    • MEAN
    • Ruby
    • DotNet
    • IoT
    • Cloud
    • Testing
    • Roku
    • CMS
    • Python
Artificial Intelligence in Healthcare

Artificial Intelligence in Healthcare

September 23, 2026

Explore how artificial intelligence in healthcare is transforming clinical decision-making, and patient care .

Read More
AI in Manufacturing: Machine Intelligence  Meets Industrial Innovation

AI in Manufacturing: Machine Intelligence Meets Industrial Innovation

September 23, 2026

Explore how AI is transforming manufacturing through predictive maintenance, intelligent quality control, data-driven operations.

Read More
Artificial Intelligence in Manufacturing: Enabling Intelligent, Flexible and Cost-Effective Production Through AI

Artificial Intelligence in Manufacturing: Enabling Intelligent, Flexible and Cost-Effective Production Through AI

September 23, 2026

Discover how AI is transforming manufacturing through intelligent automation.

Read More

India

Plot No. 11/2, Phase 3, Hinjewadi Rajiv Gandhi Infotech Park, Pune, India – 411057.
info@tudip.com
+91-96-8990-0537

United States

1999 S. Bascom Ave Suite 700, Campbell CA. 95008, USA.
info@tudip.com
+1-408-216-8162

Canada

64 Caracas Road North York, Toronto Ontario M2K 1B1, Canada.
info@tudip.com

Mexico

Calle Amado Nervo #785 Interior B Colonia Ladron De Guevara 44600 Guadalajara, Jalisco, Mexico.
info@tudip.com

Singapore

77 High Street, #10-12B High Street Plaza, Singapore 179433.
info@tudip.com

Colombia

Cra. 9 # 113-53 Of. 1405 Bogotá D.C., Colombia.
info@tudip.com

UAE

Tudip Information Technologies L.L.C Office No 109, ABU HAIL BUILDING 13, Abu Hail, Dubai, UAE.
info@tudip.com

Nigeria

22 Kumasi Crescent, Wuse 2, Abuja, Nigeria.
info@tudip.com