Pass Method as Parameter using C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the above example, the MathOperation delegate can encapsulate any method that takes two integers and returns an integer. The Add method perfectly matches this signature.
Passing Methods as Parameters Using Delegates
When you pass a method as a parameter, you typically pass the corresponding delegate. C# allows you to pass these delegates directly into methods, thereby passing the behavior you want to execute.
Example of Passing Delegate as a Parameter
In the PerformOperation method, we pass the Add method using the delegate type MathOperation. This flexibility allows different operations to be performed without modifying the PerformOperation method.
Lambda Expressions
Lambda expressions are a more concise way to define method-like behavior inline and are often used with delegates. They use the => syntax and can capture variables from the surrounding context.
Example of Lambda Expression
In this example, a lambda expression (a, b) => a - b is passed to PerformOperation method, showcasing how inline method definitions can replace traditional method references.
The System.Action and System.Func Delegates
C# provides built-in delegates Action and Func for common cases where you pass a method as a parameter. They eliminate the need to declare custom delegates for common scenarios. Action is used for methods that do not return a value, while Func is used for methods that return a value.
Example with System.Action
Example with System.Func
Table of Key Points
| Concept | Description |
| Delegate | Type-safe method pointers in C#. |
| Lambda Expression | Inline, concise method definitions using =>. |
| System.Action | Delegate for methods that do not return a value. |
| System.Func | Delegate for methods that return a value. |
| Syntax Flexibility | Enables method behavior to be passed as parameters. |
| Type Safety | Ensures delegate signature matches the method. |
Additional Topics
Anonymous Methods
Before lambda expressions were introduced, anonymous methods were used to define inline methods. Though superseded by lambdas, anonymous methods are still part of C# and can be used as:
Covariance and Contravariance
Covariance and contravariance in delegates allow a more flexible matching of method signatures, specifically related to return types and parameter types. Covariance allows a method to return a more derived type than specified in the delegate, while contravariance permits the method to accept parameters of a less derived type.
Conclusion
Passing methods as parameters using delegates and lambda expressions adds an essential level of flexibility and reusability to C# programming. By understanding and utilizing these capabilities, you can design cleaner, more efficient code through encapsulated behavior that conforms to interfaces, patterns, and functional paradigms.
Related reading
- Pass parameters to WebClient.DownloadFileCompleted event
- Passing a method parameter using Task.Factory.StartNew
- Passing a single item as IEnumerableT
- Passing arguments to C generic new of templated type
- Passing data to Master Page in ASP.NET MVC
- Passing Moq mock-objects to constructor
- Path to MSBuild
- Path.Combine absolute with relative path strings

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.