Automatic Properties
1 using System;
2
3 namespace ConsoleApplication1
4 {
5 class Program
6 {
7 public string Name { get; set; }
8
9 static void Main(string[] args)
10 {
11 var p = new Program();
12 p.Name = "Bart";
13 }
14 }
15 }
Object Initializers
using System;
class Oini
{
public static void Main()
{
Customer c = new Customer();
c.Name = "Bart";
c.City = "Ghent";
c.Age = 23;
}
}
Wouldn't it be nice to do this in just one line of code? I agree, it would be even nicer if the Customer class had a constructor that could help us, but you don't have that level of control all the time and maybe you just want to set some properties or you're out of luck with the constructors available. Therefore, C# 3.0 allows you to do this:
using System;
class Oini
{
public static void Main()
{
Customer c = new Customer() { Name = "Bart", City = "Ghent", Age = 23 };
}
}
http://community.bartdesmet.net/blogs/bart/archive/2006/12/04/C_2300_-3.0-Feature-Focus-_2D00_-Part-2-_2D00_-Object-Initializers.aspx
Collection Initializers
Object Initializers are great, and make it much easier to concisely add objects to collections. For example, if I wanted to add three people to a generics-based List collection of type "Person", I could write the below code:
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 } );
Using the new Object Initializer feature alone saved 12 extra lines of code with this sample versus what I'd need to type with the C# 2.0 compiler.
The C# and VB "Orcas" compilers allow us to go even further, though, and also now support "collection initializers" that allow us to avoid having multiple Add statements, and save even further keystrokes:
List<Person> people = new List<Person> {
new Person { FirstName = "Scott", LastName = "Guthrie", Age = 32 },
new Person { FirstName = "Bill", LastName = "Gates", Age = 50 },
new Person { FirstName = "Susanne", LastName = "Guthrie", Age = 32 }
};
When the compiler encounters the above syntax, it will automatically generate the collection insert code like the previous sample for us.
http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx
Extension Methods
Ever wanted to check to see whether a string variable is a valid email address? Today you'd probably implement this by calling a separate class (probably with a static method) to check to see whether the string is valid. For example, something like:
string email = Request.QueryString["email"];
if ( EmailValidator.IsValid(email) ) {
}
Using the new "extension method" language feature in C# and VB, I can instead add a useful "IsValidEmailAddress()" method onto the string class itself, which returns whether the string instance is a valid string or not. I can then re-write my code to be cleaner and more descriptive like so:
string email = Request.QueryString["email"];
if ( email.IsValidEmailAddress() ) {
}
How did we add this new IsValidEmailAddress() method to the existing string type? We did it by defining a static class with a static method containing our "IsValidEmailAddress" extension method like below:
public static class ScottGuExtensions
{
public static bool IsValidEmailAddress(this string s)
{
Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
return regex.IsMatch(s);
}
}
Note how the static method above has a "this" keyword before the first parameter argument of type string. This tells the compiler that this particular Extension Method should be added to objects of type "string". Within the IsValidEmailAddress() method implementation I can then access all of the public properties/methods/events of the actual string instance that the method is being called on, and return true/false depending on whether it is a valid email or not.
To add this specific Extension Method implementation to string instances within my code, I simply use a standard "using" statement to import the namespace containing the extension method implementation:
using ScottGuExtensions;
The compiler will then correctly resolve the IsValidEmailAddress() method on any string. C# and VB in the VS 2008 now provide full intellisense support for extension methods within the Visual Studio code-editor. So when I hit the "." keyword on a string variable, my extension methods will now show up in the intellisense drop-downlist:
The VB and C# compilers also naturally give you compile-time checking of all Extension Method usage - meaning you'll get a compile-time error if you mis-type or mis-use one.
http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx