# C Sharp Anonymous Functions

## C# Anonymous Functions

A function without a name is known as an anonymous function. Anonymous functions are of two types in C#:

• Lambda Expressions
• Anonymous Methods

## C# Lambda Expressions:

An anonymous function used to create delegates is called lambda expressions in C#. To create local functions to be passed as an argument, a lambda expression can be used. Also, to write LINQ queries, the lambda expression is useful.

Syntax:

(input-parameters) => expression

Example:

using System; namespace LmbdaExpr { class Example { delegate int Cube(int n); static void Main(string[] args) { Cube GetCube = a => a * a * a; int x = GetCube(10); Console.WriteLine("Cube: "+ x); } } }

Output:

Explanation:

In the above example, we are displaying the use and behavior of the lambda expression in C#.

## C# Anonymous Methods:

With the functionality the same as the lambda expression, the Anonymous method also allows the user to omit the parameter list.

Example:

using System; namespace AnymsMethd { class Example { public delegate void AnymosFun(); static void Main(string[] args) { AnymosFun x = delegate () { Console.WriteLine("Hello World!!"); }; x(); } } }

Output:

Explanation:

In the above example, we are displaying the use and behavior of the anonymous methods in C#.