C# Method Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, it's possible to pass a method as a parameter to another method. Here's how to do it with and without return values:
public int Square(int x) { return x * x; } public int PerformOperation(Func<int, int> operation, int x) { return operation(x); } // Example usage: int result = PerformOperation(Square, 5); Console.WriteLine(result); // Output: 25
In this example, the Square
method takes an integer x
and returns its square. The PerformOperation
method takes a function that takes an integer and returns an integer (Func<int, int>
) as its first parameter, and an integer x
as its second parameter. The method then calls the specified function with the provided x
parameter, and returns the result. The Square
method is then passed as the first parameter to PerformOperation
, along with the value 5
, and the resulting value is printed to the console.
public void PrintMessage(string message) { Console.WriteLine(message); } public void PerformAction(Action<string> action, string message) { action(message); } // Example usage: PerformAction(PrintMessage, "Hello, World!");
In this example, the PrintMessage
method takes a string message
and prints it to the console. The PerformAction
method takes a function that takes a string and returns nothing (Action<string>
) as its first parameter, and a string message
as its second parameter. The method then calls the specified function with the provided message
parameter, and does not return any value. The PrintMessage
method is then passed as the first parameter to PerformAction
, along with the string "Hello, World!"
, causing the message to be printed to the console.
C# pass method as parameter:
public class MethodParameterExample { public void ExecuteMethod(Action method) { // Invoke the passed method method(); } public void DisplayMessage() { Console.WriteLine("Hello, World!"); } public static void Main() { MethodParameterExample example = new MethodParameterExample(); // Pass the method as a parameter example.ExecuteMethod(example.DisplayMessage); } }
Delegate as a parameter in C# method:
public class DelegateParameterExample { public void ExecuteDelegate(Action method) { // Invoke the passed delegate method(); } public void DisplayMessage() { Console.WriteLine("Hello, World!"); } public static void Main() { DelegateParameterExample example = new DelegateParameterExample(); // Pass the method as a delegate parameter example.ExecuteDelegate(example.DisplayMessage); } }
Passing a delegate to a method in C#:
public class DelegateParameterExample { public void ExecuteDelegate(Action method) { // Invoke the passed delegate method(); } public void DisplayMessage() { Console.WriteLine("Hello, World!"); } public static void Main() { DelegateParameterExample example = new DelegateParameterExample(); // Pass the method as a delegate parameter example.ExecuteDelegate(example.DisplayMessage); } }
C# pass method reference to another method:
public class MethodReferenceExample { public void ExecuteMethod(Action method) { // Invoke the passed method method(); } public void DisplayMessage() { Console.WriteLine("Hello, World!"); } public static void Main() { MethodReferenceExample example = new MethodReferenceExample(); // Pass the method reference as a parameter example.ExecuteMethod(example.DisplayMessage); } }
Callback function in C# with method parameter:
public class CallbackExample { public void ExecuteCallback(Action callback) { // Invoke the callback method callback(); } public void DisplayMessage() { Console.WriteLine("Hello, World!"); } public static void Main() { CallbackExample example = new CallbackExample(); // Pass the method as a callback example.ExecuteCallback(example.DisplayMessage); } }
C# pass lambda expression as method parameter:
public class LambdaParameterExample { public void ExecuteLambda(Action method) { // Invoke the passed lambda expression method(); } public static void Main() { LambdaParameterExample example = new LambdaParameterExample(); // Pass a lambda expression as a parameter example.ExecuteLambda(() => Console.WriteLine("Hello, World!")); } }
Action delegate as a method parameter in C#:
public class ActionDelegateExample { public void ExecuteAction(Action action) { // Invoke the passed Action delegate action(); } public static void Main() { ActionDelegateExample example = new ActionDelegateExample(); // Pass a method as an Action delegate example.ExecuteAction(() => Console.WriteLine("Hello, World!")); } }
C# method with Func parameter:
public class FuncParameterExample { public void ExecuteFunc(Func<string> func) { // Invoke the passed Func delegate string result = func(); Console.WriteLine(result); } public string GetMessage() { return "Hello, World!"; } public static void Main() { FuncParameterExample example = new FuncParameterExample(); // Pass a method as a Func delegate example.ExecuteFunc(example.GetMessage); } }
Passing a method group as a parameter in C#:
public class MethodGroupExample { public void ExecuteMethod(Action method) { // Invoke the passed method group method(); } public void DisplayMessage() { Console.WriteLine("Hello, World!"); } public static void Main() { MethodGroupExample example = new MethodGroupExample(); // Pass the method group as a parameter example.ExecuteMethod(example.DisplayMessage); } }
C# method with Action parameter:
public class ActionParameterExample { public void ExecuteAction(Action action) { // Invoke the passed Action delegate action(); } public static void DisplayMessage() { Console.WriteLine("Hello, World!"); } public static void Main() { ActionParameterExample example = new ActionParameterExample(); // Pass a static method as an Action parameter example.ExecuteAction(DisplayMessage); } }
C# method with EventHandler parameter:
public class EventHandlerExample { public event EventHandler CustomEvent; public void RaiseEvent() { // Invoke the EventHandler CustomEvent?.Invoke(this, EventArgs.Empty); } public static void HandleEvent(object sender, EventArgs e) { Console.WriteLine("Event handled!"); } public static void Main() { EventHandlerExample example = new EventHandlerExample(); // Subscribe the method to the event example.CustomEvent += HandleEvent; // Pass the EventHandler as a parameter example.RaiseEvent(); } }
C# pass method as parameter in LINQ:
public class LinqMethodParameterExample { public void ProcessNumbers(List<int> numbers, Func<int, bool> filterMethod) { var filteredNumbers = numbers.Where(filterMethod); foreach (var number in filteredNumbers) { Console.WriteLine(number); } } public static void Main() { LinqMethodParameterExample example = new LinqMethodParameterExample(); List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Pass a method as a parameter in LINQ example.ProcessNumbers(numbers, n => n % 2 == 0); } }
C# method with Predicate parameter:
public class PredicateParameterExample { public void FilterNumbers(List<int> numbers, Predicate<int> filterMethod) { var filteredNumbers = numbers.FindAll(filterMethod); foreach (var number in filteredNumbers) { Console.WriteLine(number); } } public static bool IsEven(int number) { return number % 2 == 0; } public static void Main() { PredicateParameterExample example = new PredicateParameterExample(); List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Pass a method as a Predicate parameter example.FilterNumbers(numbers, IsEven); } }