SlideShare a Scribd company logo
C# Tutorial
Part 30 :Delegation
www.siri-kt.blogspot.com
• In C#, delegate is a reference to the method. It works
like function pointer in C and C++. But it is objected-
oriented, secured and type-safe than function pointer.
• For static method, delegate encapsulates method only. But
for instance method, it encapsulates method and instance
both.
• delegates stores the address of one or many functions
delegates hides the actual information.
• The best use of delegate is to use as event.
• Internally a delegate declaration defines a class which is the
derived class of System.Delegate.
• note:
• What is Delegates in C # Delegate is the special type use
to hold the reference of function ,Delegate concept match
the function pointer concept of c language
• C# delegates are similar to pointers to functions, in C or C++.
A delegate is a reference type variable that holds the
reference to a method. The reference can be changed at
runtime.
• Delegates are especially used for implementing events and
the call-back methods. All delegates are implicitly derived
from the System.Delegate class.
• Declaring Delegates
• Delegate declaration determines the methods that can be
referenced by the delegate. A delegate can refer to a
method, which has the same signature as that of the
delegate.
• For example, consider a delegate:
• public delegate int MyDelegate (string s);The preceding
delegate can be used to reference any method that has a
singlestring parameter and returns an int type variable.
• Syntax for delegate declaration is:
• Delegate <return type> <delegate-name> <parameter
list>Instantiating Delegates
• Once a delegate type is declared, a delegate object must be
created with thenew keyword and be associated with a
particular method.
• When creating a delegate, the argument passed to
the new expression is written similar to a method call, but
without the arguments to the method. For example:
• public delegate void printString(string s); ... printString ps1 =
new printString(WriteToScreen); printString ps2 = new
printString(WriteToFile);
• Following example demonstrates declaration, instantiation,
and use of a delegate that can be used to reference methods
that take an integer parameter and returns an integer value.
• Delegates are classified into 2 types
• 1.single cast delegate
• 2.multi cast delegate
• Here we can follow 4 steps to create a delegate
• 1.write a class with methods
• 2.create a delegate
• syntax: public delegate void dname();
• 3.stores the address of method
• dname d=new dname(stores fn address);
• 4.call the delegate variable
• d();
• 1.single cast delegate:
• The single cast delegate holds only one function address. depending on the function
prototype we have to declare delegate. in each delegate instance variable we can
stores one function address.
• example on single cast delegate example:
• using System;
• namespace ConsoleApplication21
• { class test//class
• { public void print()//method
• { Console.WriteLine("helloworld");
• Console.ReadLine(); }
• public delegate void dname();//declare delegate
• static void Main(string[] args)
• { test obj = new test();//object
• // obj.print();
• dname d = new dname(obj.print);//stores the fn address
• d();//call the delegate varibale
• } } }
• Multi cast delegate :
It holds the address of more than one function also. multi cast delegate supports
arithmatic + and – operations + operator is used to add a function into the sequence.
• - operator is used to Remove a function from the sequence.
• example on multi cast delegate:
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• namespace multicastdelegateexample
• { class emp//class name
• { public void esal()//method1
• { Console.WriteLine("from esal");
• Console.ReadLine(); }
• public void tsal()//method1
• { Console.WriteLine("from tsal");
• Console.ReadLine(); }
• Multicasting of a Delegate
• Delegate objects can be composed using the "+"
operator. A composed delegate calls the two
delegates it was composed from. Only delegates of
the same type can be composed.
• The "-" operator can be used to remove a
component delegate from a composed delegate.
• Using this property of delegates you can create an
invocation list of methods that will be called when a
delegate is invoked. This is called multicasting of a
delegate.
• public delegate void dname();//declare delagate
• static void Main(string[] args)
• { emp obj = new emp();//object
• dname d1 = new dname(obj.esal);//stores fn1 address
• // d1();
• dname d2 = new dname(obj.tsal);//stores fn2
address
• // d2();
• dname d3;
• // d3 = d1 - d2;
• d3 = d1 + d2;//multi cast delegate
• d3();//call the delegate variable
• } } }
For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching
More Angular JS TutorialsMore C sharp (c#) tutorials

More Related Content

What's hot

Java Tutorial Lab 5
Java Tutorial Lab 5Java Tutorial Lab 5
Java Tutorial Lab 5
Berk Soysal
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
Templates
TemplatesTemplates
Templates
Nilesh Dalvi
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
malaybpramanik
 
Under the hood of scala implicits (Scala eXchange 2014)
Under the hood of scala implicits (Scala eXchange 2014)Under the hood of scala implicits (Scala eXchange 2014)
Under the hood of scala implicits (Scala eXchange 2014)
Alexander Podkhalyuzin
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2
sanya6900
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
Michael Heron
 
Linq & lambda overview C#.net
Linq & lambda overview C#.netLinq & lambda overview C#.net
Linq & lambda overview C#.net
Dhairya Joshi
 
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
 
C
CC
Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Alexander Podkhalyuzin
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Hyderabad Scalability Meetup
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
baabtra.com - No. 1 supplier of quality freshers
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
Anton Kolotaev
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File ProcessingRanel Padon
 
Scala functions
Scala functionsScala functions
Scala functions
Knoldus Inc.
 
First approach in linq
First approach in linqFirst approach in linq
First approach in linq
Vignesh Nethaji
 
Templates
TemplatesTemplates
Templates
Farwa Ansari
 

What's hot (20)

Java Tutorial Lab 5
Java Tutorial Lab 5Java Tutorial Lab 5
Java Tutorial Lab 5
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Templates
TemplatesTemplates
Templates
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Under the hood of scala implicits (Scala eXchange 2014)
Under the hood of scala implicits (Scala eXchange 2014)Under the hood of scala implicits (Scala eXchange 2014)
Under the hood of scala implicits (Scala eXchange 2014)
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
Linq & lambda overview C#.net
Linq & lambda overview C#.netLinq & lambda overview C#.net
Linq & lambda overview C#.net
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
 
C
CC
C
 
Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
 
Scala functions
Scala functionsScala functions
Scala functions
 
First approach in linq
First approach in linqFirst approach in linq
First approach in linq
 
Templates
TemplatesTemplates
Templates
 

Similar to 30c

Delegetes in c#
Delegetes in c#Delegetes in c#
Delegetes in c#
Mahbub Hasan
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4thConnex
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Steve Johnson
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
SURBHI SAROHA
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
luqman bawany
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
Data Structures and Algorithms in Python
Data Structures and Algorithms in PythonData Structures and Algorithms in Python
Data Structures and Algorithms in Python
JakeLWright
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
Nico Ludwig
 
Programming Language
Programming  LanguageProgramming  Language
Programming LanguageAdeel Hamid
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
VAIBHAVKADAGANCHI
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
Java
JavaJava
Data structure Unit-I Part A
Data structure Unit-I Part AData structure Unit-I Part A
Data structure Unit-I Part A
SSN College of Engineering, Kalavakkam
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
Hashni T
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective cSunny Shaikh
 

Similar to 30c (20)

Delegetes in c#
Delegetes in c#Delegetes in c#
Delegetes in c#
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
 
Data Structures and Algorithms in Python
Data Structures and Algorithms in PythonData Structures and Algorithms in Python
Data Structures and Algorithms in Python
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Java
JavaJava
Java
 
Data structure Unit-I Part A
Data structure Unit-I Part AData structure Unit-I Part A
Data structure Unit-I Part A
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 

More from Sireesh K

Cn10
Cn10Cn10
Cn10
Sireesh K
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
Sireesh K
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
Sireesh K
 
What is mvc
What is mvcWhat is mvc
What is mvc
Sireesh K
 
31c
31c31c
31cs
31cs31cs
31cs
Sireesh K
 
45c
45c45c
44c
44c44c
43c
43c43c
42c
42c42c
41c
41c41c
40c
40c40c
39c
39c39c
38c
38c38c
37c
37c37c
35c
35c35c
34c
34c34c
33c
33c33c
29c
29c29c
27c
27c27c

More from Sireesh K (20)

Cn10
Cn10Cn10
Cn10
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
What is mvc
What is mvcWhat is mvc
What is mvc
 
31c
31c31c
31c
 
31cs
31cs31cs
31cs
 
45c
45c45c
45c
 
44c
44c44c
44c
 
43c
43c43c
43c
 
42c
42c42c
42c
 
41c
41c41c
41c
 
40c
40c40c
40c
 
39c
39c39c
39c
 
38c
38c38c
38c
 
37c
37c37c
37c
 
35c
35c35c
35c
 
34c
34c34c
34c
 
33c
33c33c
33c
 
29c
29c29c
29c
 
27c
27c27c
27c
 

Recently uploaded

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 

Recently uploaded (20)

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 

30c

  • 1. C# Tutorial Part 30 :Delegation www.siri-kt.blogspot.com
  • 2. • In C#, delegate is a reference to the method. It works like function pointer in C and C++. But it is objected- oriented, secured and type-safe than function pointer. • For static method, delegate encapsulates method only. But for instance method, it encapsulates method and instance both. • delegates stores the address of one or many functions delegates hides the actual information. • The best use of delegate is to use as event. • Internally a delegate declaration defines a class which is the derived class of System.Delegate. • note: • What is Delegates in C # Delegate is the special type use to hold the reference of function ,Delegate concept match the function pointer concept of c language
  • 3. • C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. • Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class. • Declaring Delegates • Delegate declaration determines the methods that can be referenced by the delegate. A delegate can refer to a method, which has the same signature as that of the delegate. • For example, consider a delegate: • public delegate int MyDelegate (string s);The preceding delegate can be used to reference any method that has a singlestring parameter and returns an int type variable.
  • 4. • Syntax for delegate declaration is: • Delegate <return type> <delegate-name> <parameter list>Instantiating Delegates • Once a delegate type is declared, a delegate object must be created with thenew keyword and be associated with a particular method. • When creating a delegate, the argument passed to the new expression is written similar to a method call, but without the arguments to the method. For example: • public delegate void printString(string s); ... printString ps1 = new printString(WriteToScreen); printString ps2 = new printString(WriteToFile); • Following example demonstrates declaration, instantiation, and use of a delegate that can be used to reference methods that take an integer parameter and returns an integer value.
  • 5. • Delegates are classified into 2 types • 1.single cast delegate • 2.multi cast delegate • Here we can follow 4 steps to create a delegate • 1.write a class with methods • 2.create a delegate • syntax: public delegate void dname(); • 3.stores the address of method • dname d=new dname(stores fn address); • 4.call the delegate variable • d();
  • 6. • 1.single cast delegate: • The single cast delegate holds only one function address. depending on the function prototype we have to declare delegate. in each delegate instance variable we can stores one function address. • example on single cast delegate example: • using System; • namespace ConsoleApplication21 • { class test//class • { public void print()//method • { Console.WriteLine("helloworld"); • Console.ReadLine(); } • public delegate void dname();//declare delegate • static void Main(string[] args) • { test obj = new test();//object • // obj.print(); • dname d = new dname(obj.print);//stores the fn address • d();//call the delegate varibale • } } }
  • 7. • Multi cast delegate : It holds the address of more than one function also. multi cast delegate supports arithmatic + and – operations + operator is used to add a function into the sequence. • - operator is used to Remove a function from the sequence. • example on multi cast delegate: • using System; • using System.Collections.Generic; • using System.Linq; • using System.Text; • using System.Threading.Tasks; • namespace multicastdelegateexample • { class emp//class name • { public void esal()//method1 • { Console.WriteLine("from esal"); • Console.ReadLine(); } • public void tsal()//method1 • { Console.WriteLine("from tsal"); • Console.ReadLine(); }
  • 8. • Multicasting of a Delegate • Delegate objects can be composed using the "+" operator. A composed delegate calls the two delegates it was composed from. Only delegates of the same type can be composed. • The "-" operator can be used to remove a component delegate from a composed delegate. • Using this property of delegates you can create an invocation list of methods that will be called when a delegate is invoked. This is called multicasting of a delegate.
  • 9. • public delegate void dname();//declare delagate • static void Main(string[] args) • { emp obj = new emp();//object • dname d1 = new dname(obj.esal);//stores fn1 address • // d1(); • dname d2 = new dname(obj.tsal);//stores fn2 address • // d2(); • dname d3; • // d3 = d1 - d2; • d3 = d1 + d2;//multi cast delegate • d3();//call the delegate variable • } } }
  • 10. For more visit our website www.siri-kt.blogspot.com Thanks for Watching More Angular JS TutorialsMore C sharp (c#) tutorials