Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 75: C# 3.0 Features

Section 75.1: 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

Section 75.2: 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 where x % 2 == 1 orderby x descending select x * x;

// Result: 49, 25, 1

Example from wikipedia article on C# 3.0, LINQ sub-section

Example 1 uses query syntax which was designed to look similar to SQL queries.

//Example 2

IEnumerable<int> query = array.Where(x => x % 2 == 1)

.OrderByDescending(x => x)

.Select(x => x * x);

// Result: 49, 25, 1 using 'array' as defined in previous example

Example from wikipedia article on C# 3.0, LINQ sub-section

Example 2 uses method syntax to achieve the same outcome as example 1.

It is important to note that, in C#, LINQ query syntax is syntactic sugar for LINQ method syntax. The compiler translates the queries into method calls at compile time. Some queries have to be expressed in method syntax. From MSDN - "For example, you must use a method call to express a query that retrieves the number of elements that match a specified condition."

GoalKicker.com – C# Notes for Professionals

445

Section 75.3: Lambda expresions

Lambda Expresions are an extension of anonymous methods that allow for implicitly typed parameters and return values. Their syntax is less verbose than anonymous methods and follows a functional programming style.

using System;

using System.Collections.Generic; using System.Linq;

public class Program

{

public static void Main()

{

var numberList = new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; var sumOfSquares = numberList.Select( number => number * number )

.Aggregate( (int first, int second) => { return first + second; } ); Console.WriteLine( sumOfSquares );

}

}

The above code will output the sum of the squares of the numbers 1 through 10 to the console.

The first lambda expression squares the numbers in the list. Since there is only 1 parameter parenthesis may be omitted. You can include parenthesis if you wish:

.Select( (number) => number * number);

or explicitly type the parameter but then parenthesis are required:

.Select( (int number) => number * number);

The lambda body is an expression and has an implicit return. You can use a statement body if you want as well. This is useful for more complex lambdas.

.Select( number => { return number * number; } );

The select method returns a new IEnumerable with the computed values.

The second lambda expression sums the numbers in list returned from the select method. Parentheses are required as there are multiple parameters. The types of the parameters are explicitly typed but this is not necessary. The below method is equivalent.

.Aggregate( (first, second) => { return first + second; } );

As is this one:

.Aggregate( (int first, int second) => first + second );

Section 75.4: Anonymous types

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

You can make anonymous types by using the new keyword followed by a curly brace ({). Inside the curly braces, you

GoalKicker.com – C# Notes for Professionals

446

could define properties like on code below.

var v = new { Amount = 108, Message = "Hello" };

It's also possible to create an array of anonymous types. See code below:

var a = new[] { new {

Fruit = "Apple", Color = "Red"

}, new {

Fruit = "Banana",

Color = "Yellow"

}

};

Or use it with LINQ queries:

var productQuery = from prod in products

select new { prod.Color, prod.Price };

GoalKicker.com – C# Notes for Professionals

447