0% found this document useful (0 votes)
15 views32 pages

Delegates

The document explains delegates in C#, which are types that represent references to methods with specific signatures, enabling dynamic method calls and event handling. It also covers generic delegates, differences between delegates and interfaces, and the concept of events, including their declaration, properties, and the use of built-in EventHandler delegates. Overall, it provides a comprehensive overview of how delegates and events function within the C# programming language.

Uploaded by

Jam Lomocso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views32 pages

Delegates

The document explains delegates in C#, which are types that represent references to methods with specific signatures, enabling dynamic method calls and event handling. It also covers generic delegates, differences between delegates and interfaces, and the concept of events, including their declaration, properties, and the use of built-in EventHandler delegates. Overall, it provides a comprehensive overview of how delegates and events function within the C# programming language.

Uploaded by

Jam Lomocso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

C#: Declare, Initiate, and Invoke

Delegates
Klent John D. Juaniza
What is a Delegate?
A delegate in C# is a type that represents
a reference to a method with a specific
signature (return type and parameters).
Delegates allow methods to be passed as
parameters, enabling dynamic method
calls, event handling, and callback
mechanisms.
Declare Delegates
Return Type

Access Modifier

Keyword
Name Parameters
Initiate & Invoke Delegates
Initiate & Invoke Delegates (cont..)
Multicasting
A delegate can call more than one method when invoked.
Multicasting (cont..)

Because delegate types derive from System.Delegate, the methods and


properties defined by that class on the delegate. For example, to find the
number of methods in a delegate’s invocation list.
C#: Generic Data Type
Delegates
What is a Generic Delegates Data Type?
In C#, a generic delegate is a delegate
that can be defined with type
parameters, allowing it to operate on a
variety of data types. This makes generic
delegates more flexible and reusable
compared to non-generic delegates,
which are limited to specific data types.
Generic Delegates Data Type
• Action
Represents a method that takes parameters but DOES NOT return a value.
Action<T1>, Action<T1, T2> up to 16 parameters
Generic Delegates Data Type (cont..)
• Func
Func <TResult>: Represents a method that takes parameters and returns a value.
Multiple parameters: Func <T1, TResult> up to 16 parameters
Generic Delegates Data Type (cont..)
Generic Delegates Data Type (cont..)
• Predicate
Predicate <T>: A generic delegate that takes a single parameter and returns a boolean value.
Generic Delegates Data Type (cont..)
C#: Difference between
Delegates and Interface
Reference: https://www.geeksforgeeks.org/delegates-vs-interfaces-in-c-sharp/
What is Interface?
Like a class, Interface can have methods,
properties, events, and indexers as its
members. But interfaces will contain only
the declaration of the members. The
implementation of the interface’s
members will be given by the class who
implements the interface implicitly or
explicitly.
Interface
Below are some differences between the Delegates and Interfaces in C#:

Delegate Interface

It could be a method only. It contains both methods and properties.

It can be applied to one method at a time. If a class implements an interface, then it will
implement all the methods related to that
interface.

If a delegate available in your scope you can Interface is used when your class implements
use it. that interface, otherwise not.

Delegates can me implemented any number of Interface can be implemented only one time.
times.
It is used to handling events. It is not used for handling events.
Below are some differences between the Delegates and Interfaces in C#:

Delegate Interface

It can access anonymous methods. It can not access anonymous methods.

When you access the method using delegates When you access the method you need the
you do not require any access to the object of object of the class which implemented an
the class where the method is defined. interface.

It does not support inheritance. It supports inheritance.

It can wrap static methods and sealed class It does not wrap static methods and sealed
methods class methods..

It created at run time. It created at compile time.


Below are some differences between the Delegates and Interfaces in C#:

Delegate Interface

It can implement any method that provides the If the method of interface implemented, then
same signature with the given delegate. the same name and signature method
override.

It can wrap any method whose signature is A class can implement any number of
similar to the delegate and does not consider interfaces, but can only override those
which from class it belongs. methods which belongs to the interfaces.
C#: Event Declaration and Use
Event Accessors
Reference: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/
https://www.tutorialsteacher.com/csharp/csharp-event
What is event in C#?
Events enable a class or object to notify
other classes or objects when something
of interest occurs. The class that sends
(or raises) the event is called the
PUBLISHER and the classes that receive
(or handle) the event are called
SUBSCRIBERS.
Event Declaration
An event can be declared in two steps:
• Declare a delegate.
• Declare a variable of the delegate with event keyword.

Delegate
Keyword Event Name
Raise Event
Consume Event
Events have the following properties:
• The publisher determines when an event is raised; the
subscribers determine what action is taken in response to
the event.

• An event can have multiple subscribers. A subscriber can


handle multiple events from multiple publishers.

• Events that have no subscribers are never raised.

• Events are typically used to signal user actions such as


button clicks or menu selections in graphical user interfaces.
Events have the following properties:

• When an event has multiple subscribers, the event handlers


are invoked synchronously when an event is raised.

• In the .NET class library, events are based on the


EventHandler delegate and the EventArgs base class.
Built-in EventHandler Delegate

.NET Framework includes built-in delegate types EventHandler


and EventHandler<TEventArgs> for the most common events.
Typically, any event should include two parameters: the source
of the event and event data. Use the EventHandler delegate for
all events that do not include event data. Use
EventHandler<TEventArgs> delegate for events that include
data to be sent to handlers.
In Laymans Terms
Think of EventHandler like a job description for someone who
will react to a specific event. It defines what kind of tasks
(methods) should be done when something happens.

How It Works: When you set up an event, you’re saying, "When


this thing happens, I want this job (method) to be done.“

EventHandler tells you how that job should look. Example:


Imagine you have a doorbell. When someone rings the bell, you
want to be notified. The EventHandler is like a set of instructions
for who should answer the door and what they should do when
they hear the bell.
In Laymans Terms
EventArgs is like a letter or package that contains details about
the event. It holds the information that might be needed when
reacting to the event.

How It Works: When the event happens, the letter (or


EventArgs) is handed to whoever is reacting to the event. It can
include details like "Who is at the door?" or "What time was the
bell rung?“

Example: Continuing with the doorbell analogy, the EventArgs


would be like a note that says, "The visitor’s name is John" or
"The bell was rung at 3 PM."

You might also like