Page 167 - CSharp/C#
P. 167
Chapter 22: C# 3.0 Features
Remarks
C# version 3.0 was released as part of .Net version 3.5. Many of the features added with this
version were in support of LINQ (Language INtegrated Queries).
List of added features:
• LINQ
• Lambda expressions
• Extension methods
• Anonymous types
• Implicitly typed variables
• Object and Collection Initializers
• Automatically implemented properties
• Expression trees
Examples
Implicitly typed variables (var)
The var keyword allows a programmer to implicitly type a variable at compile time. var declarations
have the same type as explicitly declared variables.
var squaredNumber = 10 * 10;
var squaredNumberDouble = 10.0 * 10.0;
var builder = new StringBuilder();
var anonymousObject = new
{
One = SquaredNumber,
Two = SquaredNumberDouble,
Three = Builder
}
The types of the above variables are int, double, StringBuilder, and an anonymous type
respectively.
It is important to note that a var variable is not dynamically typed. SquaredNumber = Builder is not
valid since you are trying to set an int to an instance of StringBuilder
Language Integrated Queries (LINQ)
//Example 1
int[] array = { 1, 5, 2, 10, 7 };
// Select squares of all odd numbers in the array sorted in descending order
IEnumerable<int> query = from x in array
https://riptutorial.com/ 113

