Freigeben über


How to create System.Delegate in J#

Creating System.Delegate in J#

 

Single-cast Delegate

 

Single-cast delegate derived from System.Delegate are created using the @delegate directive. The default delegate in J# are the ones derived from com.ms.lang.Delegate(without using the @delegate directive). If one however wants to explicitly declare a delegate of the System.Delegate type, then the @delegate directive has to be used before the delegate definition.

For example, here SampleDelegate has been declared to be of type System.Delegate :

/**@delegate*/

delegate int SampleDelegate(int a, int b);

Muti-cast Delegate

 

It needs to be noted that Single-cast and multi-cast delegates of the com.ms.lang.Delegate type are different with respect to each other. However, in case of System.Delegate type, single-cast and multi-cast delegates are not separate entities and hence when declaring multicast delegate of the System.Delegate type there is no need to use the keyword “multicast” as in case of com.ms.lang.Delegate type.

Hence, syntax for the multicast delegate here still remains the same:

/**@delegate*/

delegate int SampleDelegate(int a, int b);

Hence, single-cast and multi-cast delegate of the System.Delegate are one and the same thing. Both can be a part of the link list and also their return values can be anything. In case return value is not void, the return value of the last called method is returned.

Hence, the following program works just fine:

/**@delegate*/

delegate int SampleDelegate(int a, int b);

public class test

{

      public int greater(int a, int b)

      {

            if (a > b)

                  System.out.println("Greater");

            else

                  System.out.println("Small");

            return a;

      }

      public int lower( int a, int b)

      {

            return b;

      }

      public static void main(String args[])

      {

            test t = new test();

            SampleDelegate ch3 = new SampleDelegate(t, "greater");

            SampleDelegate ch4 = new SampleDelegate(t, "lower");

            SampleDelegate ch6 = (SampleDelegate)System.Delegate.Combine(ch3, ch4);

            System.out.println(ch6.Invoke(3, 4));

      }

}

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...