Microsoft C# .
NET
1
Programming Language
Visual Programming-I
Mr. Saifuddin Quraishi Lecturer
Jahan University, Computer Science Faculty
Kabul, Afghanistan
2
Visual Programming-I using Microsoft C# .NET
3
Chapter Five
Microsoft C# .NET Delegates
4 Lecture Agenda
Delegates and Types
Defining, Instantiating, and Invoking Delegate
Anonymous Methods
Lambda Expressions (=>)
Func, Action, and Predicate Delegates
Extension Methods
5 Lecture Objectives
To know usage of Delegates
To understanding delegates
To understand delegates instantiation
Understanding Anonymous Methods
Describes usage of Lambda Expressions (=>)
Worming with Func, Action, and Predicate Delegates
To understand Extension Methods
6 Introduction to Delegates
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.
There are two types of delegates are available in C# .NET
Single Cast Delegate
Multicast Delegate
7
Working with Delegates
Let’s go and practice
8 Defining Delegate
Consider following syntax
<accessmodifier> delegate <returntype> <delegatename>(<parameters>)
Consider following example
// Declare Delegate
public delegate void SampleDelegate(int a, int b);
9 Instantiating Delegate
Syntax:
<DelegateName> <DelegateObjectName> = new
<DelegateName>(PassTargetMethodName);
Example:
SquareDelegate sd = new SquareDelegate([Link]);
PrintDelegate pd = new PrintDelegate([Link]);
10 Invoking Delegate
There are two mechanism for invoking method via the delegate
Via the Delegate Object
Via the Pre-define Invoke Method
Defining, Instantiating, and
11
Invoking Delegate
Let’s go and practice
12 Anonymous Methods
Following is the sample way of creating anonymous methods in C# .NET
using delegate keyword.
// Create a delegate
public delegate void MathOps(int a, int b);
// Instantiate the delegate using an anonymous method
MathOps ops = delegate(int x, int y)
{
[Link]("Add Result: {0}", x + y);
[Link]("Subtract Result: {0}", x - y);
};
Lambda Expressions (=>)
13
Let’s go and practice
14 Extension Methods
To create an extension method, we need to define a static class and
implement an extension method as a static method with the required
parameters
The first parameter of the method will specify the type on which the
extension method will operate on and it must be preceded by this modifier.
15 Built-in Generics Delegates
C# .NET provides following built-in generics delegates
Func Delegate
Action Delegate
Predicate Delegate
16 Func Delegates
Syntax:
Func<InputsTypes, OutputType> ObjectName = MethodName;
Example:
Func<int, int> sd = [Link];
InputsTypes:
Specify the type of inputs parameters in a sequence in which they placed in
method declaration
More than one type separated by commas
OutputType:
Specify the type of returning value from a method
17 Action Delegates
Syntax:
Action<InputsTypes> ObjectName = MethodName;
Example:
Action<int, float, double> smd = Sum;
InputsTypes:
Specify the type of inputs parameters in a sequence in which they placed in
method declaration
More than one type separated by commas
18 Predicate Delegates
Syntax:
Predicate<InputType> ObjectName = MethodName;
Example:
Predicate<double> rd = Result;
Remember!
Predicate delegate has only one input parameter
19
Built-in Generics Delegates
Let’s go and practice
20 Summary
In C#, delegate is a reference to the method
There are two mechanisms are available for delegate invoking
To create an extension method, we need to define a static class
Func, Action, and predicate are built-in generics delegates