2017-10-13 108 views
0

如果我有一個方法將委託作爲參數,傳入委託(即指向方法)和傳入方法名稱之間是否有區別?如果是這樣,每種方法的優點/缺點是什麼?C#傳入委託與傳入方法

delegate void MyDel(); 
static void MyFunc(MyDel) 
{ 
MyDel(); 
} 
static void SomeFunc(){//do stuff} 
static void Main() 
{ 
MyDel del= new MyDel(SomeFunc); 

//What's the difference between the approaches below? 
del(); 
MyFunc(SomeFunc); 
} 
+3

的可能的複製[是代表和回調相同或相似的?](https://stackoverflow.com/questions/290819/are-代表和回調 - 相同或相似) – GolezTrol

+2

什麼是*「通過實際方法」*?你能舉兩個例子的代碼嗎? – Sinatr

+2

沒有「傳入實際方法」這樣的東西 - 如果您使用*方法組*作爲參數,它將轉換爲委託。 –

回答

-3

委託用於調用方法threadsafe。他們是類型安全的。 直接傳遞方法意味着如果您更改了例如Form.Control可能會導致應用程序崩潰。爲此,你需要一個委託

好吧,我把它送回去:)

+0

請閱讀:https://stackoverflow.com/questions/290819/are-delegates-and-callbacks-the-same-or-similar –