2013-02-09 41 views
5

Tôi đã tự hỏi nếu C# đại biểu mất một số tiền tương tự của không gian mà C con trỏ (4 byte) làm khi đi đến một phương pháp.C# tham số đại biểu kích thước

Chỉnh sửa

đại biểu chỉ trỏ đến phương pháp đúng? họ không thể chỉ vào cấu trúc hoặc lớp học tôi đúng.

Trả lời

0

Có, đại biểu chỉ trỏ đến phương pháp, một hoặc nhiều. Các tham số phải tương tự như phương pháp.

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

} 

}

Các vấn đề liên quan