.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

A simple delegate program in C#

Posted By:Abhisek Panda       Posted Date: November 27, 2009    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  
 

A simple delegate program in C#

using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleDelegate
{
// This delegate can point to any method
// taking two integers and returning an integer.
public delegate int BinaryMath(int x, int y);

public class SimpleMath
{
public int Add(int x, int y)
{ return x + y; }
public int Subtract(int x, int y)
{ return x - y; }
public static int SquareNumber(int a)
{ return a * a; }
}

class SimpleDelegate
{
static void Main(string[] args)
{
Console.WriteLine("***** A Simple Delegate Use *****\n");
// Create a BinaryOp object that
// 'points to' SimpleMath.Add()
SimpleMath m = new SimpleMath();
BinaryMath b = new BinaryMath(m.Add);

// Show information about this object.
DisplayDelegateInfo(b);
// Invoke Add() method using delegate.
Console.WriteLine("\n15 + 30 is {0}", b(15, 30));
Console.ReadLine();
}

static void DisplayDelegateInfo(Delegate delObj)
{
// Print the names of each member in the
// delegates invocation list.
foreach (Delegate d in delObj.GetInvocationList())
{
Console.WriteLine("Method Name: {0}", d.Method);
Console.WriteLine("Target Name: {0}", d.Target);
}
}
}
}

Description:-


Here the format of BinaryMath delegate is that, it can point to any method taking two integer value and returning an integer value . We have created a class SimpleMath which contains two static methods that match the pattern of the delegate.

Then we are creating a object of the BinaryMath delegate which will point to the desired method in the SimpleMath class.

We have simply pass the name of the method to the delegate's constructor. The Invoke() method is actually called here,

 Console.WriteLine("\n15 + 30 is {0}", b(15, 30));
.NET delegates are type safe. If you will attempt to pass a delegate a method that does not "match the pattern", you will receive a compile time error. Suppose the SimpleMath class contains another method as SquareNo() as follows,
public class SimpleMath
{
public static int SquareNo(int a)
{
return a* a;
}
}

If you will create the BinaryMath objectr which will point to SquareNo() method it will give the error "Method does not match delegate pattern! ". as follows,

BinaryMath b = new BinaryMath(SimpleMath.SquareNo(20));
         Just add another method named DisplayDelegateInfo() . It will print the names of the methods maintained by the System.Delegate derived type as well as the name of the class defining the method. To do this we have to iterate over the System.Delegate array returned by GetInvocationList(), invoking each object's target and method properties.
 static void DisplayDelegateInfo(Delegate delObj)
{
// Print the names of each member in the
// delegates invocation list.
foreach (Delegate d in delObj.GetInvocationList())
{
Console.WriteLine("Method Name: {0}", d.Method);
Console.WriteLine("Target Name: {0}", d.Target);
}
}
          In the output the type will not displayed by the Target property if our delegate points to the static method and therefore there id no object to reference. To display the type name just create an object of simple method class and call the method using this type as follows,
 // Create a BinaryOp object that points to SimpleMath.Add()
SimpleMath m = new SimpleMath();
BinaryMath b = new BinaryMath(m.Add);
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend