---
title: "Lambda Expression in c#."  
description: "In this blog we will see that how to use lambda expression in c#. Here I will also display that how to use annoymouse expression and you will see the"  
author: "Anonymous User"  
published: 2011-05-21  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Lambda Expression in c#.

A [lambda expression](https://www.mindstick.com/articles/897/lambda-expression-in-c-sharp) is an [anonymous function](https://www.mindstick.com/interview/23492/what-is-an-anonymous-function-in-javascript) that can contain expressions and statements and can be used to create delegates or expression tree type. All [lambda expressions](https://www.mindstick.com/forum/159557/c-plus-plus-lambda-expressions-how-does-the-compiler-interpret-them) use the lambda operator =>, which is read as “goes to”. The left side of lambda operator specifies the input parameter if any and right side holds the expression or statement block.

The lambda expression has the same precedence as [assignments](https://yourviews.mindstick.com/view/82699/mysql-assignments) (=) and is right associative. We can use lambdas in method based [LINQ query](https://www.mindstick.com/forum/33908/how-to-select-and-retrieve-data-using-linq-query-in-entity-framework) as argument to slandered query. We cannot use lambda operators on the left side of is or [as operator](https://www.mindstick.com/interview/23208/what-is-difference-between-is-and-as-operator).

We can use following [basic form](https://www.mindstick.com/forum/156618/basic-form-with-the-help-of-bootstrap-classes) of expression using lambda expression

1) (input parameters) => expression: Here parenthesis are optional if there is only one parameter otherwise it is required.

2) (x, y)=>x==y: We can use this type of lambda expressions when we want to specify input types to the compiler.

3) ()=>SomeMethod(): When we want to create expression trees such as with [sql server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) function then we use these type of lambda expression.

##### Example which represents lambda expression with collection

```
/// <summary>        /// In this method we will see use of two method one is use of         /// annoymouse method to search value in collection and other one        /// is by using lambda expression to search. We see that using lambda        /// expression line of code is saved.        /// </summary>        public void lambdaExpression()        {            //Create a collection objects.            List<string> nameCollection = new List<string>();            //Add some values in collection object.            nameCollection.Add("James");            nameCollection.Add("Chow");            nameCollection.Add("John");            nameCollection.Add("Haider");            nameCollection.Add("Amit");            //Using annoymouse expression for checking name            string result = nameCollection.Find(delegate(string name)            {                return name.Equals("John");            });            if (!string.IsNullOrEmpty(result))                Console.WriteLine(result);            else                Console.WriteLine("Specified record is not found.");            //Using lambda expression for checking name            string lambdaResult = nameCollection.Find(sname => sname.Equals("James1"));    //Here we directly write name of variable like sname.            if (!string.IsNullOrEmpty(lambdaResult))                Console.WriteLine(lambdaResult);            else                Console.WriteLine("Specified record is not found.");        }
```

---

Original Source: https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
