Some Insights into Delegates from J# perspective

It needs to be noted that the classes, System.Delegate or System.Multicast Delegate are all abstract classes. Hence, we cannot create instances of or System.Delegate classes with the “new” keyword.

When we declare a delegate using the “delegate” keyword, compiler generates a non-abstract class derived from the type if @delegate is not used else from the class is derived from System.Delegate type. Since, this delegate is a non-abstract class, it can go as the upper level class. Also, now you can create their instances with the “new” keyword supplying them necessary instantiating parameters as required.

However, if you use syntax like: del1;

System.Delegate del2;

They are only references to the corresponding types and hence cannot be used as the top level class. They need to go inside some class as its member. However, they can be definitely assigned the reference to delegate object whose delegate class was generated through the “delegate” keyword and that was instantiated with the new” keyword.

Last but not the least, Delegate class itself does not have a invoke method. This method is injected by the compiler while generating a non-abstract delegate class when it encounters the “delegate” keyword. Hence, the only way to create a delegate class (which is non-abstract) during compilation is to use the “delegate” keyword.

For example following code snippet works just fine.

// compiler would generate SampleDelegate as a non-abstract

// class derived from

delegate void SampleDelegate(int a, int b);

// commented since its just a refrence

// hence, cannot keep it here

/* del; */

public class test { del;

      public void greater( int a, int b ) {

            if (a > b)





      public static void main(String args[])


        test t = new test();

      // Commented as this would give error

      // since Delegate here is an abstract class

            /* t.del = new; */

            t.del = new SampleDelegate(t.greater);

            // won't work b'se itself does not

            // have invoke member method, this method is generated

            // by the compiler into the newly generated class during compilation

            /* t.del.invoke(21, 20); */

            ((SampleDelegate)(t.del)).invoke(21, 20);



                  t.del.dynamicInvoke(new Object[] { new Integer(21), new Integer(20) });


            catch (Exception e)







You can also refer to the following link for more info on Delegates :