Difference between a lambda expression and a delegate in C#In this article, you will learn about the difference between a lambda expression and a delegate in C#. But before discussing their differences, you must know about the lambda expression and a delegate with their syntax and examples. What is the Delegate?A reference type representing a method with a particular signature in C# is called a delegate. It allows indirect method invocation and acts essentially as a reference to a function. Delegates enable methods to be stored in data structures, returned from methods, and passed as parameters. Syntax:It has the following syntax: Parameters:
Example:Let us take an example to illustrate the delegate in C#. Output: The result of the Addition of two numbers: 30 The result of the Subtraction of two numbers: -10 Explanation: A delegate called MathOperationsDelegate represents the methods in this C #code that accept two double parameters and return a double result. The Demo class declares two static methods, Addition and Subtraction that carry out addition and subtraction operations, respectively. Addition and Subtraction are assigned to delegate instances created in the Main Method. The associated methods are then called after these delegate instances are called with parameters 10 and 20. Lastly, the console displays the results of the addition and subtraction operations. This code demonstrates how to use delegates to allow dynamic behaviour in method calls by providing a level of indirection for method invocation. What is the Lambda Expression?In C#, lambda expressions provide a simple syntax for composing anonymous functions or methods. They are shortcuts for defining expressions or delegates that may be assigned to variables or passed as arguments to methods. Syntax:It has the following syntax: Parameters: The parameters are denoted by parenthesis (). For the lambda expression, they stand in for the input parameters. Need to use empty brackets () if there are no arguments. Lambda Operator (=>): The lambda operator, sometimes referred to as the "goes to" operator, separates the lambda expression's body into its parameters. It comprises the equals sign '=' and the greater-than symbol '>'. Expression: Any expression enclosed in curly braces {} can be specified after the lambda operator.
Example:Let us take an example to illustrate the Lambda Expression in C#. Output: The square of 10 is: 100 The sum of the 26 and 18 is: 44 Lambda Expression! The sign of -15 is: Non-positive. The result of (14 * 18) + 19 is: 271 Explanation: The short and expressive way these lambda expressions enable the definition of inline functions improves the readability and maintainability of C# code. They demonstrate how lambda expressions may be flexibly used to handle different situations and types of operations. Difference between a lambda expression and a delegate in C#In C# programming, both lambda expressions and delegates are necessary. While delegates offer a mechanism for encapsulating method references, callback mechanisms, and event-driven programming, lambda expressions offer a clearer and more expressive way to define inline functions.
Next TopicDifferent ways to access namespace in C# |