2010-06-25 45 views
5

如何傳遞參數作爲函數傳遞函數作爲函數的參數

+2

你能更清楚了嗎? – ckv 2010-06-25 04:10:04

+0

請參閱下面的鏈接已經在SO http://stackoverflow.com/questions/380198/how-to-pass-a-function-as-a-parameter-in-c – ckv 2010-06-25 04:15:05

回答

6

您可能正在尋找delegates

public delegate void MyDelegate(int myInt, string myString); 
public void FunctionToCall(int i, string s) 
{ 
    Console.WriteLine(s + " [" + i.ToString() + "]"); 
} 
public void MethodWithFunctionPointer(MyDelegate callback) 
{ 
    callback(5, "The value is: "); 
} 

,然後把它稱爲:

MethodWithFunctionPointer(FunctionToCall); 
1

使得參數爲代表,並調用函數的函數地址應與委託匹配