luni, 23 noiembrie 2009

[c#] C# 3.0 Features

Implicitly Typed Arrays
With respect to arrays in C# 2.0, only the length could be inferred from the simple form of array initializer. Implicit typing has since been extended to arrays so that both the element type and the length are inferred from the expression, as in:

var GroupsOfStats = new[] {
new {Name = "Stat Group 1";
Stats = new [] { "Rates Rx", "Rates Tx", "First Timestamp"} ,
new {Name = "Stat Group 2";
Stats = new [] { "Rates Rx", "Rates Tx", "Last Timestamp", "Loss%"}

This would create a new array of a type that has two rows (row 0 and row 1). Each element is an object with two fields, Name and Stats. Members itself is an array with a different length in each case.

Object and Collection Initializers
Initializers specify values for fields or properties of objects or collections. Examples of initializers include:
Point p = new Point {X = 0, Y = 1};
List m_digits = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
In C# 2.0, initialization syntax was valid only for arrays.

Anonymous Types
Anonymous types are created from object initializers. An anonymous type is a nameless class type that inherits directly from the supertype object. The members of an anonymous type are a sequence of read-only properties inferred from the anonymous object initializer used to create an instance of the type. By nesting new declarations, an initializer can specify values for an entire collection of objects. For example, the anonymous type created by this declaration:
var GroupsOfStats = new[] {
new {Name = "Stat Group 1";
Stats = new [] { "Rates Rx", "Rates Tx", "First Timestamp"} ,
new {Name = "Stat Group 2";
Stats = new [] { "Rates Rx", "Rates Tx", "Last Timestamp", "Loss%"}
would have two properties, Name and Stats, with Members being an array of strings. The types would have internal names and would be compatible with any other type of the same structure.
Automatic get properties are created for the listed members, so we can access the value of group.Name. There is no set property.

Lambda Expressions
In fact, the lambda calculus – a very simple language where everything is expressed in terms of functions.
A lambda expression simply defines an anonymous function. A function is something that takes one or more parameters (just as a method does) and uses them in computing some value. That value becomes the return value for the function. In C# 3, the "=>" syntax is used to write a lambda expression. You place the parameters to the left of the arrow and the expression to compute to the right.

x => x + 1
(x, y) => x * y
() => new Beer()

(x, y) => {
var result = x + y;
return result;}

var FindTags = new Regex(@"\[(/?)(\w+)\]");
string Output = FindTags.Replace(ToRender,
m => AcceptedTags.Contains(m.Groups[2].Value) ?         
"<" + m.Groups[1].Value + m.Groups[2].Value + ">" :

One difference you may have spotted between lambda expressions and the original anonymous method syntax is the absence of types on the
parameters. You actually can write the types in if you wish:

(int x, int y) => x + y

Niciun comentariu:

Trimiteți un comentariu