Delegates

Views:
 
Category: Education
     
 

Presentation Description

C#.net

Comments

Presentation Transcript

Delegates:

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

PowerPoint Presentation:

Delegate Method 1 Method 2 Method 3 Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Use of delegate:

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:

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:

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:

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:

Defining the instance methods Class operation{ int add(int a,int b) { int z; r eturn(z); } i nt 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:

Defining the static methods Class operation{ s tatic int add(int a,int b) { int z; r eturn(z); } s tatic int subtract(int a , int b) { return(a-b); } s tatic int mult (int a , int b) { return(a*b); } } Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Initializing the delegate:

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

Initializing the delegate:

Initializing the delegate //with static methods Create an object of the delegate delop d=new delop(add); d elop d1=new delop(subtract); d elop d2=new delop( mult ); Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

PowerPoint Presentation:

Delop a dd subtract mult Run Run Run Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Invoking the delegate:

Invoking the delegate d(20,30); //output 50 d1(30,20); //output 10 d 2 (6,7); //output 42 Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Program2 Example:

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); } } Delegate definition Function definition Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

PowerPoint Presentation:

class main { public static void Main() { 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 initialisation Delegate Invocation Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

PowerPoint Presentation:

Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

PowerPoint Presentation:

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:

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

PowerPoint Presentation:

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:

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:

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

Features of delegates:

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:

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

PowerPoint Presentation:

Delop a dd subtract mult Run Prof Indrani sen,Assistant professor,Nirmala memorial foundation college

Program Example:

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

Output:

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

Delegates using Arrays:

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

Output:

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

authorStream Live Help