Microsoft released Visual Studio 2008 together with the .NET 3.5 framework and C# 3.
There are some new exciting features included in this release.
Here are some of them:
1. "var" - You can assign any object to a var (for instance: var i = 1). This is not the same as using "object" because the compiler (and VS) will figure out the actual type and make sure you don't make mistakes.
You can see how VS figures out that "j" is of type string and autocompletes the correct methods:
2. Simplified properties - Instead of having to write getters and setters blocks for class properties, you simply have to declare a "get; set;" block. For example the class User:
The compiler will automatically create the needed class fields.
3. Class and collection initializers - after you defined your class properties you can easily create a new class and initialize it with default properties.
So the class User can be instantiated like this:
4. Extension methods (duck typing in other languages) - You can add methods to an already existing class. You define a new extension method and the compiler will attach your method to any type that matches the definition - of course once you define the extension method, VS will become aware of it and will autocomplete it for you.
In the example below I define an extension method for the type "string". I want to add the "encrypt" method to string, so first I define the extension method - this is a simple method that encrypts a string but notice the encrypt method signature:
public static string encrypt(this string plaintext, string password)
It must be static and have the keyword "this" before the first argument. The "this" keyword will instruct the compiler to attach the encrypt method to the "string" class (string is the parameter type of the first argument).
Notice that the class is defined within the "Technital" namespace so only strings that are used inside this namespace will be affected (like strings in the User class defined before).
Here I use the extension method on the Password property from the User class (a string). You can see VS picked up the extension method and now offers it in the autocomplete box.
So here is the final result:
5. Anonymous types - You can declare anonymous types. They are treated as "object" and the compiler will internally assign them with a name. This is a feature which is used quite a lot with LINQ
6. Lambda expressions - C# allows using lambda expressions (which are just another way to define anonymous functions). They are defined like this:
And are especially useful when used together with LINQ (which I guess is the real reason why this feature was introduced):
Now comes the obvious comparison to Java...
When comparing these features (and considering how young C# is) you can't help but wonder how long will Java be able to hold its head above the water? I mean java stakeholders are still debating if stuff like delegates and properties should be included in JDK 7 (you can find all kinds of missing features here)
Not to mention killer features like extension methods or LINQ. We can only dream of having these in Java.
It seems like Microsoft are aware of the popularity of dynamic languages like Ruby - you can see they borrow a lot of the new features from such languages. They have developers in mind.
Sun on the other hand, are thinking of Mathematicians - not developers. They are so careful of making sure that every aspect of the Java language is mathematically valid that they forget that they are dealing with innovative software developers that want their favorite development language to be innovative as well.
The innovative developer that I am (or at least want to be) - I guess my favorite language can't be Java anymore.
Come on Sun, wake up and smell the Java.