After reading a post by Scott Guthrie on the new C# Orcas language features I decided to download Orcas and try them out. (excited)
The new features I practiced with and love are:
- Object initializers
Example:
Prior to Orcas:
1
2
3
4
| Person person = new Person();
person.FirstName = "Scott";
person.LastName = "Guthrie";
person.Age = 32;
|
Orcas:
1
| Person person = new Person { FirstName="Scott", LastName="Guthrie", Age=32 };
|
I personally like this because I like to have a constructor initializer for all my objects. That is no longer necessary. =)
- Collection initilizers
Orcas:
1
2
3
4
5
| List<Person> people = new List<Person>();
people.Add( new Person { FirstName = "Scott", LastName = "Guthrie", Age = 32 } );
people.Add( new Person { FirstName = "Bill", LastName = "Gates", Age = 50 } );
people.Add( new Person { FirstName = "Susanne", LastName = "Guthrie", Age = 32 } );
|
The new feature I played with that I’m still not to sure about is:
- Automatic Properties
Example:
Prior to Orcas:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| public class Person {
private string _firstName;
private string_lastName;
private int _age;
public string FirstName {
get {
return _firstName;
}
set {
_firstName = value;
}
}
public string LastName {
get {
return _lastName;
}
set {
_lastName = value;
}
}
public int Age {
get {
return _age;
}
set {
_age = value;
}
}
}
|
Orcas:
1
2
3
4
5
| public class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
|
Guthrie’s post has links to Bart deSmart’s blog where he shows what happens under the covers for each of these features.
More to come.