We help IT Professionals succeed at work.

Delegate in c#

rnsr
rnsr asked
on
498 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

Commented:
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
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
More examples of where you could use delegates is with Event handlers, Linq, and callbacks (async).

Author

Commented:
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.

Author

Commented:
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

Commented:
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

Author

Commented:
I am busy and will write comment soon.
Thanks

Author

Commented:
nice to learn
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.