SlideShare a Scribd company logo
Delegates, Lambdas
         and Events
Delegates
Anonymous Methods
Lambda Expressions
Events
Delegate
 A delegate in C# is similar to a function pointer in
 C or C++.

 Using a delegate allows the programmer to
 encapsulate a reference to a method inside a
 delegate object.
Declaring Delegate
 Defining a delegate means telling the compiler what
  kind of method a delegate of that type will represents.


  Syntax for Delegate:
     <Access Specifier> delegate <returntype> delgatename(params);



  Ex:
   public delegate int PerformCalculation(int x, int y);
Types of Delegates
 Single Cast Delegate
    A Single-cast derives from the System.Delegate class. It
     contains reference to one method only at a time.
 Multi Cast Delegate
    A multicast delegate derives from the
     System.MulticastDelegate class. It contains an invocation
     list of multiple methods.
    In multicasting a single delegate invokes multiple
     encapsulated methods. The return type of all these
     delegates is same.
Action<T> and Func<T> Delegates
 Action<T>
    The generic Action<T> delegate is meant to reference a
     method with void return.
    EX:
      Action<in T1, in T2 …. in T16> (T1..T16 are input parameters)

 Func<T>
    Func<T> allows you to invoke methods with a return type.
    Ex:
        Func<in T1,in T2…,out Res>
           (T1,T2 etc are input parameters and Res is output parameter)
Anonymous Methods
 Creating anonymous methods is essentially a
 way to pass a code block as a delegate
 parameter.
   Ex:
     delegate void Del(int x);
     Del d = delegate(int k) { /* ... */ };

 Using anonymous methods, reduce the coding
 overhead in instantiating delegates by eliminating
 the need to create a separate method.
Lambda Expressions
 A lambda expression is an anonymous function that
 can contain expressions and statements, and can be
 used to create delegates or expression tree types.

 All lambda expressions use the lambda operator =>,
 which is read as "goes to".
   Ex:
        delegate void TestDelegate(string s);
                …
       TestDelegate myDel = n => { string s = n + " " + "World";
      Console.WriteLine(s); };
Events
 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.
Pictorial Representation of Events
 The publisher determines when an event is
 raised.

 The subscribers determine what action is taken in
 response to the event.

   Creating a Event:
       public event EventHandler<ClassName> Event-Name
        {
           add{//………}
           remove{//…………}
         }
Ad

More Related Content

What's hot (20)

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
Sarfaraz Ghanta
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Function in C program
Function in C programFunction in C program
Function in C program
Nurul Zakiah Zamri Tan
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
Danial Mirza
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
Haitham El-Ghareeb
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 

Viewers also liked (20)

C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event Handling
Jussi Pohjolainen
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and events
Iblesoft
 
Deferred Execution (延遲執行)
Deferred Execution (延遲執行)Deferred Execution (延遲執行)
Deferred Execution (延遲執行)
Zheng-Wei Lin
 
Delegate (委派) Introduction
Delegate (委派) IntroductionDelegate (委派) Introduction
Delegate (委派) Introduction
Zheng-Wei Lin
 
C# Delegates, Events, Lambda
C# Delegates, Events, LambdaC# Delegates, Events, Lambda
C# Delegates, Events, Lambda
Jussi Pohjolainen
 
Asynchronous programming from Xamarin Hakcday in Melbourne
Asynchronous programming from Xamarin Hakcday in MelbourneAsynchronous programming from Xamarin Hakcday in Melbourne
Asynchronous programming from Xamarin Hakcday in Melbourne
Filip Ekberg
 
Async in .NET
Async in .NETAsync in .NET
Async in .NET
RTigger
 
Evolution of C# delegates
Evolution of C# delegatesEvolution of C# delegates
Evolution of C# delegates
mbaric
 
CTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & AwaitCTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & Await
Spiffy
 
Async/Await
Async/AwaitAsync/Await
Async/Await
Jeff Hart
 
Chapter 6 OOP (Revision)
Chapter 6 OOP (Revision)Chapter 6 OOP (Revision)
Chapter 6 OOP (Revision)
OUM SAOKOSAL
 
Using Async in your Mobile Apps - Marek Safar
Using Async in your Mobile Apps - Marek SafarUsing Async in your Mobile Apps - Marek Safar
Using Async in your Mobile Apps - Marek Safar
Xamarin
 
Fluent interface in c#
Fluent interface in c#Fluent interface in c#
Fluent interface in c#
Dror Helper
 
Total oop in c# dot net
Total oop in c# dot netTotal oop in c# dot net
Total oop in c# dot net
Muhammad Naveed
 
interface in c#
interface in c#interface in c#
interface in c#
Deepti Pillai
 
01 Ajax Intro
01 Ajax Intro01 Ajax Intro
01 Ajax Intro
Dennis Pipper
 
Java swing
Java swingJava swing
Java swing
profbnk
 
Introduction To Silverlight and Prism
Introduction To Silverlight and PrismIntroduction To Silverlight and Prism
Introduction To Silverlight and Prism
tombeuckelaere
 
2310 b 09
2310 b 092310 b 09
2310 b 09
Krazy Koder
 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event Handling
Jussi Pohjolainen
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and events
Iblesoft
 
Deferred Execution (延遲執行)
Deferred Execution (延遲執行)Deferred Execution (延遲執行)
Deferred Execution (延遲執行)
Zheng-Wei Lin
 
Delegate (委派) Introduction
Delegate (委派) IntroductionDelegate (委派) Introduction
Delegate (委派) Introduction
Zheng-Wei Lin
 
C# Delegates, Events, Lambda
C# Delegates, Events, LambdaC# Delegates, Events, Lambda
C# Delegates, Events, Lambda
Jussi Pohjolainen
 
Asynchronous programming from Xamarin Hakcday in Melbourne
Asynchronous programming from Xamarin Hakcday in MelbourneAsynchronous programming from Xamarin Hakcday in Melbourne
Asynchronous programming from Xamarin Hakcday in Melbourne
Filip Ekberg
 
Async in .NET
Async in .NETAsync in .NET
Async in .NET
RTigger
 
Evolution of C# delegates
Evolution of C# delegatesEvolution of C# delegates
Evolution of C# delegates
mbaric
 
CTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & AwaitCTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & Await
Spiffy
 
Chapter 6 OOP (Revision)
Chapter 6 OOP (Revision)Chapter 6 OOP (Revision)
Chapter 6 OOP (Revision)
OUM SAOKOSAL
 
Using Async in your Mobile Apps - Marek Safar
Using Async in your Mobile Apps - Marek SafarUsing Async in your Mobile Apps - Marek Safar
Using Async in your Mobile Apps - Marek Safar
Xamarin
 
Fluent interface in c#
Fluent interface in c#Fluent interface in c#
Fluent interface in c#
Dror Helper
 
Java swing
Java swingJava swing
Java swing
profbnk
 
Introduction To Silverlight and Prism
Introduction To Silverlight and PrismIntroduction To Silverlight and Prism
Introduction To Silverlight and Prism
tombeuckelaere
 
Ad

Similar to C# Delegates (20)

Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
Abed Bukhari
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Jaliya Udagedara
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
Prem Kumar Badri
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
.NET F# Events
.NET F# Events.NET F# Events
.NET F# Events
DrRajeshreeKhande
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Pranali Chaudhari
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
Prem Kumar Badri
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
Generics_RIO.ppt
Generics_RIO.pptGenerics_RIO.ppt
Generics_RIO.ppt
Ponnieaswari M.S
 
Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambda
John Walsh
 
Interface
InterfaceInterface
Interface
Ashwini Yadav
 
CSharp_04_Events-in-C#-introduction-with-examples
CSharp_04_Events-in-C#-introduction-with-examplesCSharp_04_Events-in-C#-introduction-with-examples
CSharp_04_Events-in-C#-introduction-with-examples
Ranjithsingh20
 
Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3
Ali Raza Zaidi
 
Delegate
DelegateDelegate
Delegate
rsvermacdac
 
PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...
PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...
PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...
Khushboo Jain
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Bc0037
Bc0037Bc0037
Bc0037
hayerpa
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
Demystifying Type Class derivation with Shapeless
Demystifying Type Class derivation with ShapelessDemystifying Type Class derivation with Shapeless
Demystifying Type Class derivation with Shapeless
Yurii Ostapchuk
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
Abed Bukhari
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Jaliya Udagedara
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
Prem Kumar Badri
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambda
John Walsh
 
CSharp_04_Events-in-C#-introduction-with-examples
CSharp_04_Events-in-C#-introduction-with-examplesCSharp_04_Events-in-C#-introduction-with-examples
CSharp_04_Events-in-C#-introduction-with-examples
Ranjithsingh20
 
Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3
Ali Raza Zaidi
 
PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...
PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...
PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...
Khushboo Jain
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
Demystifying Type Class derivation with Shapeless
Demystifying Type Class derivation with ShapelessDemystifying Type Class derivation with Shapeless
Demystifying Type Class derivation with Shapeless
Yurii Ostapchuk
 
Ad

More from Raghuveer Guthikonda (9)

C# String
C# StringC# String
C# String
Raghuveer Guthikonda
 
Operators & Casts
Operators & CastsOperators & Casts
Operators & Casts
Raghuveer Guthikonda
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
Raghuveer Guthikonda
 
Generics C#
Generics C#Generics C#
Generics C#
Raghuveer Guthikonda
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
Raghuveer Guthikonda
 
Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
Raghuveer Guthikonda
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
Raghuveer Guthikonda
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
Raghuveer Guthikonda
 
Regex in C#
Regex in C#Regex in C#
Regex in C#
Raghuveer Guthikonda
 

Recently uploaded (20)

Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Learn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step GuideLearn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step Guide
Marcel David
 
Buckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug LogsBuckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug Logs
Lynda Kane
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Rock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning JourneyRock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning Journey
Lynda Kane
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Automation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From AnywhereAutomation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From Anywhere
Lynda Kane
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.
gregtap1
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Buckeye Dreamin 2024: Assessing and Resolving Technical Debt
Buckeye Dreamin 2024: Assessing and Resolving Technical DebtBuckeye Dreamin 2024: Assessing and Resolving Technical Debt
Buckeye Dreamin 2024: Assessing and Resolving Technical Debt
Lynda Kane
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
"PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System""PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Learn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step GuideLearn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step Guide
Marcel David
 
Buckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug LogsBuckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug Logs
Lynda Kane
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Rock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning JourneyRock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning Journey
Lynda Kane
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Automation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From AnywhereAutomation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From Anywhere
Lynda Kane
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.
gregtap1
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Buckeye Dreamin 2024: Assessing and Resolving Technical Debt
Buckeye Dreamin 2024: Assessing and Resolving Technical DebtBuckeye Dreamin 2024: Assessing and Resolving Technical Debt
Buckeye Dreamin 2024: Assessing and Resolving Technical Debt
Lynda Kane
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
"PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System""PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 

C# Delegates

  • 1. Delegates, Lambdas and Events Delegates Anonymous Methods Lambda Expressions Events
  • 2. Delegate  A delegate in C# is similar to a function pointer in C or C++.  Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object.
  • 3. Declaring Delegate  Defining a delegate means telling the compiler what kind of method a delegate of that type will represents. Syntax for Delegate:  <Access Specifier> delegate <returntype> delgatename(params); Ex:  public delegate int PerformCalculation(int x, int y);
  • 4. Types of Delegates  Single Cast Delegate  A Single-cast derives from the System.Delegate class. It contains reference to one method only at a time.  Multi Cast Delegate  A multicast delegate derives from the System.MulticastDelegate class. It contains an invocation list of multiple methods.  In multicasting a single delegate invokes multiple encapsulated methods. The return type of all these delegates is same.
  • 5. Action<T> and Func<T> Delegates  Action<T>  The generic Action<T> delegate is meant to reference a method with void return.  EX:  Action<in T1, in T2 …. in T16> (T1..T16 are input parameters)  Func<T>  Func<T> allows you to invoke methods with a return type.  Ex:  Func<in T1,in T2…,out Res> (T1,T2 etc are input parameters and Res is output parameter)
  • 6. Anonymous Methods  Creating anonymous methods is essentially a way to pass a code block as a delegate parameter.  Ex:  delegate void Del(int x);  Del d = delegate(int k) { /* ... */ };  Using anonymous methods, reduce the coding overhead in instantiating delegates by eliminating the need to create a separate method.
  • 7. Lambda Expressions  A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.  All lambda expressions use the lambda operator =>, which is read as "goes to".  Ex: delegate void TestDelegate(string s); … TestDelegate myDel = n => { string s = n + " " + "World"; Console.WriteLine(s); };
  • 8. Events  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.
  • 10.  The publisher determines when an event is raised.  The subscribers determine what action is taken in response to the event.  Creating a Event:  public event EventHandler<ClassName> Event-Name { add{//………} remove{//…………} }