Share via


Some Insights into Delegates from J# perspective

It needs to be noted that the classes com.ms.lang.Delegate, System.Delegate or System.Multicast Delegate are all abstract classes. Hence, we cannot create instances of com.ms.lang 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 com.ms.lang.Delegate 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:

com.ms.lang.Delegate 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 com.ms.lang.Delegate

delegate void SampleDelegate(int a, int b);

// commented since its just a refrence

// hence, cannot keep it here

/* com.ms.lang.Delegate del; */

public class test {

      com.ms.lang.Delegate del;

      public void greater( int a, int b ) {

            if (a > b)

            System.out.println("Greater");

            else

            System.out.println("Small");

      }

      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 com.ms.lang.Delegate(); */

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

            // won't work b'se com.ms.lang.Delegate 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);

            try

            {

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

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

      }

}

 

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

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/bjthedel.asp

Comments

  • Anonymous
    March 17, 2006
    While I was trying to write a blog around the topic of Delegates in J#, I found that a few blogs have...