Hi everybody
I want to know what does this code do, why do they put a delegate? please explain me the topics and concepts behind every word
Thanks :)
Converter<int, String> converter = new Converter<int, String>(delegate(int value){return Convert.ToString(value);});
Loading
Angel RomeroPosted Jun 26, 2009, 9:59 AM
I wanted to identify the concepts behind that code snippet, "Anonymous method" is a good clue to keep on reading
Thanks!
naura paxPosted Jun 26, 2009, 8:27 AM
I guess it is an anynomous function call which is passed as parameter in
the constructor on Converts class.
The constructor requires you to pass a reference to a method defined by you that
contains the logic to convert Tinput value to Toutput value
as in Converts(TInput,TOutput).
The same thing can be achived by following code.
Converter<int, String> converter = new Converter<int, String>(chk);
// A new Function defintion that converts integer to string.
private string chk(int val)
{
return val.ToString();
}
delegate has been used to create an anonymous method so that you don't have to define a concrete function as above.
I hope i'm making some sense here.
Please respond and comment on my post. I would appreciate that