Wednesday, February 18, 2009

Five favourite new features in VS 2008

1. Extension methods

This feature I like most.
Thanks that you can extend standard object methods

Lets look below example:



As you noticed only difference is “this” word near parameter.
Here you are how to use this:




Please also note that if you add extension to object type – all variable types will get access to this extension!

My favourite example (coming from Scott Gu’s blog http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx )





2. Object Initializers

This is feature I waited long. It gives you possibility to assign in one line public properties in braces after constructor, like this:




3. Automatic Properties

If you don’t like all extra effort you need to do to write fields and properties code you can use below snippet.



CLR 3.x (during compilation) will generate fields and property code for you.
Note: you will not be able to access generated field (only way is only through setters and getters)

4. Local variable type inference

This feature allows you define local variables without defining their types (similar to variant variables in some cases), so you can do this:




Compiler generates appropriate int, string and “SomeType” variables in early-bound strongly-typed fashion.

Personally, I’d prefer explicit types definitions – this gives more clarity in code.
But in some cases (mainly for shorten long code) I’m finding it usefull:



5. Lambdas

Described soon...




Other nice features:
- partial methods

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home