Vous êtes sur la page 1sur 27

Delegates

A delegate is a reference type that defines a method signature, and can be associated with any method with a compatible signature. We can invoke (or call) the method through the delegate. Delegates are used to pass methods as arguments .
Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Delegate

Method 1

Method 2

Method 3

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Use of delegate

Any method from any accessible class or struct that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. The method can be either static or an instance method. This makes it possible to programmatically change method calls(at run time), As long as we know the signature of the delegate, we can assign our own method.
Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Rules of using Delegate

In the context of method overloading, the signature of a method does not include the return value. But in the context of delegates, the signature does include the return value. In other words, a method must have the same return value as the delegate.
Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Steps to work with a delegate

Declaring a Delegate Defining the methods referenced by a Delegate. Initialization of a Delegate. Invoking a Delegate.

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Declaring a Delegate

Syntax: delegate <return type> <delegate name>(parameter list); Delegate int delop(int,int);

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Defining the instance methods

Class operation{ int add(int a,int b) { int z; return(z); } int subtract(int a , int b) { return(a-b); }

int mult(int a , int b) { return(a*b); } }

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Defining the static methods

Class operation{ static int add(int a,int b) { int z; return(z); } static int subtract(int a , int b) { return(a-b); }

static int mult(int a , int b) { return(a*b); } }

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Initializing the delegate

//with instance methods Create an object of the class operation o=new operation(); Create an object of the delegate delop d=new delop(o.add); delop d1=new delop(o.subtract); delop d2=new delop(o.mult);
Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Initializing the delegate

//with static methods Create an object of the delegate delop d=new delop(add); delop d1=new delop(subtract); delop d2=new delop(mult);

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Delop

add

subtract

mult

Run

Run
Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Run

Invoking the delegate

d(20,30); //output 50 d1(30,20); //output 10 d2(6,7); //output 42

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Program2 Example

using System; delegate int delop(int a,int b); class op { public int add(int a,int b) { return(a+b); } public int subtr(int a,int b) { return(a-b); } public int mult(int a,int b) { return(a*b); } Prof Indrani sen,Assistant }

Delegate definition

Function definition

professor,Nirmala memorial foundation college

class main { Delegate public static void Main() initialisation { op o=new op(); delop d=new delop(o.add); int x,y; Console.WriteLine ("Enter 2 numbers to be added:"); x=Int32.Parse (Console.ReadLine ()); y=Int32 .Parse (Console .ReadLine ()); Console.WriteLine ("sum="+d(x,y));

Console.WriteLine ("Enter 2 numbers to be subtracted:"); x=Int32.Parse (Console.ReadLine ()); y=Int32 .Parse (Console .ReadLine ()); d = new delop(o.subtr); Console.WriteLine ("difference="+ d(x,y)); Console.WriteLine ("Enter 2 numbers to be multiplied:"); x=Int32.Parse (Console.ReadLine ()); y=Int32 .Parse (Console .ReadLine ()); d=new delop (o.mult); Console.WriteLine ("product="+d(x,y)); } } Delegate Invocation

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

A program which uses two functions:Concat :- for concatenating two strings. Reverse:- for reversing a string. A delegate delstr is used to call those functions

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Program example

using System; delegate string delstr(string s1); class test { public static string rev(string s1) { string s2 = null; int j = 0; for (int i = s1.Length - 1; i >= 0; i--) { s2 = s2 + s1[i]; j++; } return (s2); Prof Indrani sen,Assistant } professor,Nirmala memorial foundation
college

public static string concat(string s1) { Console.WriteLine("Enter the string to be concatenated with:"); string s2 =Console.ReadLine(); s2 = s2 + s1; return (s2); }

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Program example

public static void Main() { delstr ds = new delstr(rev); string source, dest; Console.WriteLine("Enter the original string:"); source = Console.ReadLine(); dest = ds(source); Console.WriteLine(dest); ds = new delstr(concat); dest = ds(source); Console.WriteLine(dest); } }
Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Output

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Features of delegates

Delegates have the following properties: Delegates are like C++ function pointers but are type safe. Delegates allow methods to be passed as parameters. Delegates can be chained together; for example, multiple methods can be called on a single event.

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Multicasting

A useful property of delegate objects is that they can be assigned to one delegate instance to be multicast using the + operator. A composed delegate calls the 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. Multicasting can only be done on methods having void return type.
Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Delop

add

subtract

mult

Run
Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Program Example

using System; class main delegate void delop(int a, int b); { class op public static void Main() { { public void add(int a, int b) op o = new op(); { delop d; Console.WriteLine("sum={0}", delop d1 = new delop(o.add); a+b); delop d2 = new delop(o.subtr); } delop d3 = new delop(o.mult); public void subtr(int a, int b) int x, y; { Console.WriteLine("Enter 2 numbers ") Console.WriteLine("difference=" + x = Int32.Parse(Console.ReadLine()); (a-b)); y = Int32.Parse(Console.ReadLine()); } public void mult(int a, int b) d = d1; Multicasting { d += d2; Console.WriteLine("product={0}", d += d3; a * b); d(x, y); } } } Prof Indrani sen,Assistant professor,Nirmala } memorial foundation
college

Output

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Delegates using Arrays


using System; class main public delegate void del(); { class test public static void Main() { { public void yellow() test t = new test(); { del[] d = new[] { new del(t.yellow), new del(t.red), new del(t.green) }; Console.WriteLine ("Yellow light signals are to get ready"); d[0](); } d[1](); public void red() d[2](); { Console.WriteLine ("Red light } signals are to get ready"); } public void green() } { Console.WriteLine ("Green light signals are to get ready"); } } Prof Indrani sen,Assistant
professor,Nirmala memorial foundation college

Output

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Vous aimerez peut-être aussi