In C#, you can pass a function as a parameter to another method using a delegate or a functional interface. Here's an example of how to do it using a delegate:
public class MyMath { public int Compute(int x, int y, Func<int, int, int> operation) { return operation(x, y); } } // Usage example public static void Main() { var myMath = new MyMath(); int sum = myMath.Compute(2, 3, (a, b) => a + b); int difference = myMath.Compute(5, 2, (a, b) => a - b); Console.WriteLine($"Sum: {sum}"); Console.WriteLine($"Difference: {difference}"); }
In this example, MyMath
is a class that defines a Compute
method that takes two int
values and a Func<int, int, int>
delegate that represents a function that takes two int
values and returns an int
value. The Compute
method invokes the function passed as the operation
parameter, passing in the x
and y
parameters.
In the Main
method, an instance of MyMath
is created, and the Compute
method is called twice, passing in different functions that compute the sum and difference of the two int
parameters. The results of the computations are printed to the console.
By passing a function as a parameter to the Compute
method, you can make the method more flexible and allow it to perform different operations depending on the function passed in. This can be useful in a variety of situations, such as sorting or filtering data.
"C# pass function as parameter example"
// Example 1: Passing a Function as a Parameter public delegate void MyDelegate(string message); public static void DisplayMessage(string message) { Console.WriteLine($"Message: {message}"); } public static void ProcessFunction(MyDelegate myDelegate, string message) { myDelegate(message); } // Usage ProcessFunction(DisplayMessage, "Hello, passing a function in C#!");
"C# higher-order functions tutorial"
// Example 2: Higher-Order Function in C# public static void HigherOrderFunction(Action<string> action, string data) { Console.Write("Before: "); action(data); Console.Write("After: "); action(data.ToUpper()); } // Usage HigherOrderFunction(DisplayMessage, "Passing a function");
"Anonymous functions as parameters in C#"
// Example 3: Anonymous Function as Parameter public static void ProcessAnonymousFunction(Action<string> action, string data) { action(data); } // Usage ProcessAnonymousFunction(delegate (string message) { Console.WriteLine($"Anonymous Function: {message}"); }, "C# is powerful!");
"Delegate functions in C#"
// Example 4: Using Delegate for Function Parameter public delegate void DisplayDelegate(string message); public static void ShowMessage(DisplayDelegate displayDelegate, string message) { displayDelegate(message); } // Usage ShowMessage(DisplayMessage, "Delegate function example");
"Func and Action delegates in C#"
// Example 5: Using Func and Action Delegates public static void ProcessFuncDelegate(Func<string, string> func, string data) { var result = func(data); Console.WriteLine($"Result: {result}"); } // Usage ProcessFuncDelegate(s => $"Processed: {s.ToUpper()}", "Function with Func delegate");
"C# passing method as parameter"
// Example 6: Passing Method as Parameter public static void ExecuteMethod(Action method) { Console.WriteLine("Executing method..."); method(); } public static void SampleMethod() { Console.WriteLine("Sample method called."); } // Usage ExecuteMethod(SampleMethod);
"C# lambda expressions as function parameters"
// Example 7: Lambda Expression as Function Parameter public static void ProcessWithLambda(Action<string> action, string data) { action(data); } // Usage ProcessWithLambda((message) => Console.WriteLine($"Lambda Expression: {message}"), "Lambda in C#");
"C# passing a function to another class"
// Example 8: Passing Function to Another Class public class FunctionProcessor { public void ProcessFunction(Action<string> action, string data) { action(data); } } // Usage var processor = new FunctionProcessor(); processor.ProcessFunction(DisplayMessage, "Function passed to another class");
"C# method group as parameter"
// Example 9: Method Group as Parameter public static void ApplyMethodGroup(Action<string> action, string data) { action(data); } // Usage ApplyMethodGroup(DisplayMessage, "Method group as parameter");
"C# passing a function to a thread"
// Example 10: Passing Function to Thread public static void ThreadFunction(object obj) { Action<string> action = (Action<string>)obj; action("Function executed in a thread"); } // Usage var thread = new Thread(ThreadFunction); thread.Start(new Action<string>(DisplayMessage));
uicollectionviewlayout ms-access-2010 word-boundary many-to-many numeric-input pyodbc blobs angularjs-authentication calllog message-listener