C# 3.0 – New features – Part 1

January 20, 2007 § 2 Comments

If you listened to the two channel 9 videos I posted you might have heard a lot about functional programming, Lambda expressions, LINQ and so on. It was overwhelming to hear so many terms all at once and not know what exactly each meant. On top of that the speakers talked about multi-core and concurrency and my head started spinning, so I decided I would learn each of the terms mentioned and even better record it for others and myself for future reference.

Instead of going into the details of different terms used above let’s see first hand what exactly we will be able to do in C# 3.0.

var customers = BusinessLogic.GetCustomers();

The above statement gives a sense of dynamism to C#. You would be thinking that you no longer have to specify the Type hence C# has become like VB where customer can be of any Type. While the first conclusion is correct, the second one is not. With the above statement the compiler tries to infer the Type using the right side of the expression. If it cannot figure it out, it throws an error. Also, after the above statement, if you write the following statement the compiler will throw an error:

customers = 10;

Although this syntactic sugar makes C# appear like a dynamic language, in reality it is actually statically Typed. Also note that you can only use the var keyword with local variables. There are a few other restrictions and you can read some of them here. There have been many times when I have felt a bit retarded to type the Type of a class two times. For Example Customer c = new Customer(); instead var c = new Customer(); makes it much simpler.

The real reason var was added is so that we can declare a strongly typed variable without needing to know the name of the variable’s type. This is required in order to enable another new C# 3.0 language feature: anonymous types. You wouldn’t be able to declare a variable of an anonymous type if you always had to include the type name as part of the variable declaration. That’s the main reason var has been added to the language.

– from this article by Ian Griffiths

More to come …

Advertisements

§ 2 Responses to C# 3.0 – New features – Part 1

  • Vaibhav says:

    All features in C# 3.0 are additions and MS is not taking away any of the existing features. Everything is still strongly typed and hence type safe, the compiler does the type inferencing at compile time and throws an error if it cannot. As for the reason for having anonymous types, it is not a feature that they created for us, instead we got it as a bonus. The real reason for var and anonymous types is LINQ. LINQ let’s you query your objects just like you would query a database. Querying objects lets you write simpler code as opposed to writing big for loops with if statements.

  • Vaibhav says:

    One of the benefits of Object-Oriented language is type safe.  By allowing anonymous type, it ruins this benefit.  Perhaps I missed the point why an anonymous type is needed.  One thing for sure is that I would strongly object using it in my codes (that’s the only thing I can control, unfortunately).  I am hoping c# 3.0 would still allow me to use Customer c = new Customer().  Otherwise I probably need to shop for another language.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading C# 3.0 – New features – Part 1 at vaibhavkamath.info.

meta

%d bloggers like this: