Link to home
Start Free TrialLog in
Avatar of rnsr
rnsrFlag for India

asked on

Delegate in c#

I want to know when and why should i use delegate in my program in c#.
Is it only to encapsulates a reference to a method.
also what does it mean delegate( a= x+y ).
The url has been checked by me -
http://msdn.microsoft.com/en-us/library/ms173173(v=vs.80).aspx
but not clear
Avatar of oxyoo
oxyoo
Flag of Sweden image

A delegate could be used when..

An eventing design pattern is used
It is desirable to encapsulate a static method
The caller has no need to access other properties, medhods, or interfaces on the object implementing the method
Easy composition is desired
A class may need more than one implementation of the method

Ref: http://msdn.microsoft.com/en-us/library/ms173173(v=vs.80).aspx
ASKER CERTIFIED SOLUTION
Avatar of oxyoo
oxyoo
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
More examples of where you could use delegates is with Event handlers, Linq, and callbacks (async).
Avatar of rnsr

ASKER

Refer to it -
  MyDelegate del1 = new MyDelegate(MyClass.Add);
 int result = del1(5,5);

this example i had taken from web site.
here i am using function add which is the method of class MyClass.
I had specified the Method add .
so where am i hiding the function.

Plz help me to clear the concept.
Avatar of rnsr

ASKER

I need some clearification  about  with example in c# if possible --
-- The caller has no need to access other properties, medhods, or interfaces on    
    the object implementing the method
-- Easy composition is desired
--  A class may need more than one implementation of the method

Thanks
Sorry for late response (easter weekend :o) )...
Here's is an attempt to see if I can clarify delegates a bit more...

I have created a Logger class, this class contains a delegate (LogHandler), it is the LogHandler's responsibility to log whatever the user wants. To make this more flexible (in the future, multiple different logger might be needed) the user must provide a log handler for us (sending it in via the constructor).

Then there are the two Implemented Logger classes (Console and FormattedConsole), which we will use to log the messages in a slightly different way.

We can now, easily, create loggers with different behavior without the need to change the Logger class. Just pass any of the loggers in as delegates into the constructor of the Logger.


Hope this helps!

using System;

public class Logger {
  public delegate void LogHandler(string message);

  private readonly LogHandler _logHandler;

  public Logger(LogHandler logHandler) {
    if(logHandler == null ) throw new ArgumentException("logHandler not correctly initialized.");
    _logHandler = logHandler;
  }

  public void Log(string message) {
    _logHandler(message);
  }
}


public class ConsoleLogger {
  public static void Logger(string message) {
    Console.WriteLine(message);
  }
}

public class FormattedConsoleLogger {
  public static void Logger(string message) {
    Console.WriteLine(string.Format("Someone logged: {0}", message));
  }
}


public class Delegate {
  public void Execute() {
    var logger = new Logger(ConsoleLogger.Logger);
    var formattedLogger = new Logger(FormattedConsoleLogger.Logger);


    logger.Log("Hello, World!");
    formattedLogger.Log("Hello, World!");
  }
}

Open in new window

Avatar of rnsr

ASKER

I am busy and will write comment soon.
Thanks
Avatar of rnsr

ASKER

nice to learn