2013-02-09 7 views
5

C#デリゲートがメソッドに渡すときにCのポインタ(4バイト)と同じ量のスペースを占めるかどうかは疑問でした。C#デリゲートのパラメータサイズ

編集

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

答えて

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!"); 

} 

}

関連する問題