Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Anonymous delegate

Tags:

c#

delegates

Like Anonymous Methods ,the delegates i am declaring down using "delegate" keyword are anonymous delegates?

namespace Test
{
    public delegate void MyDelegate();
    class Program
    {
        static void Main(string[] args)
        {
            DelegateTest tst = new DelegateTest();
            tst.Chaining();
            Console.ReadKey(true);
        }
    }

    class DelegateTest
    {
        public event MyDelegate del;

        public void Chaining()
        {
            del += delegate { Console.WriteLine("Hello World"); };
            del += delegate { Console.WriteLine("Good Things"); };
            del += delegate { Console.WriteLine("Wonderful World"); };
            del();
        }
    }
}
like image 919
user160677 Avatar asked Nov 17 '09 13:11

user160677


1 Answers

There's no such thing as an "anonymous delegate" (or rather, that's not a recognised term in the C# specification, or any other .NET-related specification I'm aware of).

There are anonymous functions which include anonymous methods and lambda expressions.

Your code shows plain old anonymous methods - although they are using the one feature lambda expressions don't have: the ability to not express the parameters at all when you don't care about them.

like image 104
Jon Skeet Avatar answered Sep 20 '22 12:09

Jon Skeet