Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have seen so place .net code is using => symbol.For example:
e => e.LocalName

What doe it mean?
I have seen it in Linq syntax also. What is this actually ?
Posted

it is called lambda operator, parameter specify in the left side and the right side of the operator you can specify the expression/ statement block.
read : Lambda Expressions (C# Programming Guide)[^]
CP articles:
- Basics of LINQ & Lamda Expressions[^]
- Exploring Lambda Expression in C#[^]
- Way to Lambda[^]
- C# Delegates, Anonymous Methods, and Lambda Expressions – O My![^]
 
Share this answer
 
v2
A Lambda expressions are a simpler syntax for anonymous delegates and can be used everywhere an anonymous delegate can be used. However, the opposite is not true; lambda expressions can be converted to expression trees which allows for a lot of the magic like LINQ to SQL.

For Example

C#
List<int> elements = new List<int>() { 10, 20, 31, 40 };
	// ... Find index of first odd element.
	int oddIndex = elements.FindIndex(x => x % 2 != 0);</int></int>



Explanation :

x The argument name.
=> Separates argument list from result expression.
x % 2 !=0 Returns true if x is not even.

Second example
C#
// anonymous delegate
var evens = Enumerable
                .Range(1, 100)
                .Where(delegate(int x) { return (x % 2) == 0; })
                .ToList();

// lambda expression
var evens = Enumerable
                .Range(1, 100)
                .Where(x => (x % 2) == 0)
                .ToList();



In MVC model => item.FirstName means. Logically it would translate to string Function(Model model) { return item.FirstName; }. Basically this is a function with a parameter, but this parameter is not used.

And now, the last bit of the information. Although lambda expressions represent functions in the end, sometimes they are created not with the purpose of actually being executed (although potentially they can). A lambda expression can be represented in form of an expression tree. This means that instead of executing it it can be parsed.

In this particular case MVC engine does not run the function that the lamba expression represents. Instead the expression is parsed so that MVC engine knows what html to emit for this particular line. If your data item does not come from your model object directly, the left part of the lambda expression does not matter.

For more information, Please see following links

http://msdn.microsoft.com/en-IN/library/bb397687.aspx[^]

http://www.dotnetperls.com/lambda[^]

http://stackoverflow.com/questions/167343/c-sharp-lambda-expression-why-should-i-use-this[^]

Way to Lambda[^]


http://www.dotnet-tricks.com/Tutorial/csharp/LOUM180612-C-Sharp-Lamda-Expression.html[^]
 
Share this answer
 
Comments
Am Gayathri 15-Dec-14 8:08am    
Thanks a lot
Manoj Kumar Choubey 15-Dec-14 9:08am    
welcome :)
"=>" is an operator.

A detailed explanation as well as an example can be found here:
http://msdn.microsoft.com/de-de/library/bb311046.aspx[^]
 
Share this answer
 
Its a Lambda Expression.

Reference: Understand Lambda Expressions in 3 minutes
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900