Hi,
Anonymous methods allow code blocks to be written "in-line" where
delegate values are expected. Anonymous methods are similar to lambda
functions in the Lisp programming language.
Look into this code:
public delegate void MyDelegate(string myString);
public void MyMethod(string myString) { Console.WriteLine(myString);.}
MyDelegate delgt = MyMethod;
Instead using anonymous method we can "in-line" the delegate and the
method block.
public delegate void MyDelegate(string myString);
MyDelegate delgt = delegate (string myString) {
Console.WriteLine(myString);
};
But one feature of anonymous method is that if the delegate declaration
expects some parameters to pass, then also we can assign an anonymous
method without any parameters to that delegate. this holds true if the
Delegate declaration does not expect any out/ref parameters.
For more info visit this URL:
http://msdn2.microsoft.com/en-us/library/0yw3tz5k.aspx
Cheers!!
Dipankar
Brian Hampson wrote:
> I'm TOTALLY in the dark regarding "anonymous" methods. What
> advantages do they have?
>
> Dipankar wrote:
> > Hi Brian,
> >
> > If you are using C# 2.0 you can do so using Anonymous methods.
> >
> > Create a delegate like this:
> >
> > public delegate void MyMethodDelegate(string myString);
> >
> > Create two anonymous methods like this:
> >
> > MyMethodDelegate delgt1 = delegate(string myString) {
> >
> > }
> >
> > MyMethodDelegate delgt2 = delegate {
> >
> > }
> >
> > Cheers!!
> > Dipankar
> > Bangalore, India
> >
> > Brian Hampson wrote:
> > > I'm new to this delegate thing, and I need to use it to do UI updates
> > > from a backgroundworker thread.
> > >
> > > I have a method with overloaded signatures of:
> > >
> > > void MyMethod(string MyString);
> > > void MyMethod();
> > >
> > > How can I define (a) delegate(s) for this function without having to
> > > resort to:
> > >
> > > delegate void MyMethodDelegate1(string MyString);
> > > delegate void MyMethodDelegate2();
> > >
> > > Thanks.
> > >
> > > Brian Hampson
> > > System Administrator, North America
> > > ALS Group