Understand Lambda Expressions in 3 minutes
2013-06-25 12:26
316 查看
http://www.codeproject.com/Tips/298963/Understand-Lambda-Expressions-in-3-minutes
What is a Lambda Expression?
A lambda expression is an anonymous function and it is mostly used to create delegates in LINQ. Simply put, it's a method without a declaration, i.e., access modifier, return value declaration, and name.
Why do we need lambda expressions? (Why would we need to write a method without a name?)
Convenience. It's a shorthand that allows you to write a method in the same place you are going to use it. Especially useful in places where a method is being used only once, and the method definition is short. It saves you the effort of declaring and writing
a separate method to the containing class.
Benefits:
Reduced typing. No need to specify the name of the function, its return type, and its access modifier.
When reading the code you don't need to look elsewhere for the method's definition.
Lambda expressions should be short. A complex definition makes the calling code difficult to read.
How do we define a lambda expression?
Lambda basic definition: Parameters => Executed code.
n is the input parameter
n % 2 == 1 is the expression
You can read n => n % 2 == 1 like: "input parameter named n goes to anonymous function which returns true if the input is odd".
Same example (now execute the lambda):
That's all, now you know the basics of Lambda Expressions.
* I didn't mention expression trees/run time advantages of lambda expression due to limited scope.
* Reference
Lambda Expression tutorial
What is a Lambda Expression?
A lambda expression is an anonymous function and it is mostly used to create delegates in LINQ. Simply put, it's a method without a declaration, i.e., access modifier, return value declaration, and name.
Why do we need lambda expressions? (Why would we need to write a method without a name?)
Convenience. It's a shorthand that allows you to write a method in the same place you are going to use it. Especially useful in places where a method is being used only once, and the method definition is short. It saves you the effort of declaring and writing
a separate method to the containing class.
Benefits:
Reduced typing. No need to specify the name of the function, its return type, and its access modifier.
When reading the code you don't need to look elsewhere for the method's definition.
Lambda expressions should be short. A complex definition makes the calling code difficult to read.
How do we define a lambda expression?
Lambda basic definition: Parameters => Executed code.
Simple example
n => n % 2 == 1
n is the input parameter
n % 2 == 1 is the expression
You can read n => n % 2 == 1 like: "input parameter named n goes to anonymous function which returns true if the input is odd".
Same example (now execute the lambda):
List<int> numbers = new List<int>{11,37,52}; List<int> oddNumbers = numbers.where(n => n % 2 == 1).ToList(); //Now oddNumbers is equal to 11 and 37
That's all, now you know the basics of Lambda Expressions.
* I didn't mention expression trees/run time advantages of lambda expression due to limited scope.
* Reference
Lambda Expression tutorial
相关文章推荐
- Understand Lambda Expressions in 3 minutes(翻译)
- Understand Lambda Expressions in 3 minutes(翻译)
- 什么是lambda expressions in java
- Recursive Lambda Expressions in C#
- Lambda Expressions in Java 8
- How to Use Lambda Expressions in a Query
- Regular Expressions in 10 Minutes
- Lambda Expressions in C++
- Lambda Expressions in C++
- Functional genomics to understand and manipulate salinity tolerance in plants -- 2010年8月3日 9:00
- Understand Your Project Costs in an Engineer-to-Order Manufacturing Environment
- 4. Lambda Expressions (Lambda表达式)与Expressions Tree(表达式树)
- Creating a Ruby Weblog in 10 Minutes
- JSTL的问题(According to TLD or attribute directive in tag file, attribute value does not accept any expressions)
- According to TLD or attribute directive in tag file, attribute value does not accept any expressions
- Maven in 5 Minutes(Windows)
- C#中Lambda Expressions和 Anonymous Methods
- vscode 中 安装插件 vetur 之后报错 The Vue Language Server server crashed 5 times in the last 3 minutes. The s
- Building OrderBy Lambda Expression from Property Name in C#
- lambda expressions are not supported at this language level错误