subbiah
subbiah

Reputation: 91

passing function as argument in the function

how to pass argument as function

Upvotes: 5

Views: 681

Answers (2)

drharris
drharris

Reputation: 11214

You're probably looking for 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: ");
}

And then, to call it:

MethodWithFunctionPointer(FunctionToCall);

Upvotes: 6

IBhadelia
IBhadelia

Reputation: 291

Make argument as delegate, and call function with address of function which should match with delegates

Upvotes: 1

Related Questions