2009-10-23 82 views
0

根據動作委託的定義,它不返回值但通過值。動作委託 - 聲明更正

我傳遞值Console.WriteLine()

Action<int> an = new Action<int>(Console.WriteLine(3000)); 

但我仍然收到錯誤的方法名expected.What是什麼問題?

回答

2

你會這樣的代碼是:

Action<int> an = new Action<int>(Console.WriteLine); 
an(3000); 

克里斯

3

Action<int>的構造函數期望您傳遞一個指針,該指針將一個整數作爲參數並不返回任何內容。你傳遞的不是一個函數,而是一個表達。您既可以定義一個匿名函數或使用現有的一個:

Action<int> an = new Action<int>(t => Console.WriteLine(t)); 
an(3000); 
0

行動點的方法不僅沒有任何參數。

然後,您可以使用它像這樣調用操作:

Action<int> action = new Action<int>(Console.WriteLine); 
action.Invoke(3000);