Delegates Method & Target

This is just for me so I can easily find example notes.

One of my prime resources for learning C#:
Jamie King on YouTube

...

using System;

/// <summary>
/// Delegates keep track of "method" and "target"
/// </summary>
namespace ScratchPad
{

    delegate void MeDelegate();
    // Compiler:
    //class MeDelegate {}

    class MainClass
    {
        static void Main()
        {
            MeDelegate del = Foo;
            // Compiler:
            //MeDelegate del = new MeDelegate(Foo);

            del();
            // Compiler:
            //del.Invoke();

            InvokeTheDelegate(del);
            InvokeTheDelegate(Goo);

            Console.WriteLine("STATIC:");
            Console.WriteLine("del.Method: " + del.Method);
            Console.WriteLine("del.Target: " + del.Target);

            var m = new MainClass();
            del = m.Soo;
            Console.WriteLine("INSTANCE:");
            Console.WriteLine("del.Method: " + del.Method);
            Console.WriteLine("del.Target: " + del.Target);

            InvokeTheDelegate(del);

        }

        static void InvokeTheDelegate(MeDelegate deler)
        {
            deler();
        }

        // static
        static void Foo()
        {
            Console.WriteLine("Foo()");
        }

        // static
        static void Goo()
        {
            Console.WriteLine("Goo()");
        }

        // instance
        void Soo()
        {
            Console.WriteLine("Soo()");
        }

    }
}

Foo()
Foo()
Goo()
STATIC:
del.Method: Void Foo()
del.Target:
INSTANCE:
del.Method: Void Soo()
del.Target: ScratchPad.MainClass
Soo()
Press any key to continue . . .

Print Friendly, PDF & Email