5

メソッドに渡すときに、C# デリゲートが C ポインター (4 バイト) と同じ量のスペースを占有するかどうか疑問に思っていました。

編集

デリゲートはメソッドのみを指しますよね? 構造体やクラスを指すことはできません。私は正しいです。

4

1 に答える 1

0

はい、デリゲートは 1 つ以上のメソッドのみを指します。パラメーターは、メソッドに似ている必要があります。

public class Program
{
public delegate void Del(string message);
public delegate void Multiple();

public static void Main()
{
    Del handler = DelegateMethod;
    handler("Hello World");

    MethodWithCallback(5, 11, handler);

    Multiple multiplesMethods = MethodWithException;
    multiplesMethods += MethodOk;


    Console.WriteLine("Methods: " + multiplesMethods.GetInvocationList().GetLength(0));

    multiplesMethods();
}

public static void DelegateMethod(string message)
{
    Console.WriteLine(message);
}

public static void MethodWithCallback(int param1, int param2, Del callback)
{
    Console.WriteLine("The number is: " + (param1 + param2).ToString());
}

public static void MethodWithException()
{
    throw new Exception("Error");
}

public static void MethodOk()
{
    Console.WriteLine("Method OK!");

}

}

于 2015-07-15T18:06:36.093 に答える