2013-02-09 85 views
5

我在想如果C#委託在傳遞給方法時佔用C指針(4字節)所做的類似數量的空間。C#委託參數大小

編輯

僅代表點的方法嗎?他們不能指向結構或類我是否正確。

回答

0

是的,委託只指向方法,一個或多個。 參數必須與方法相似。

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

} 

}