Another stop on my whirlwind tour of C# awaits you on Microsoft's Channel 9. You can find it at Whirlwind 5: What's new in C# 3 - Automatically Implemented Properties, Type Inference, Initializer (10:56).
Since this is the first whirlwind session that explores the wonderful world of C# 3.0, I lead off with a brief timeline of .NET Framework, C#, and Visual Studio releases — platform, language, and tools. One thing is clear, Microsoft has continued to innovate on all three of these elements that affect the code we write and how we write it. And they intend for the .NET Framework to be a platform on which they will continue to innovate, so expect a significant release every year or two. Language releases are probably less frequent, and tools somewhere between the two. Embrace change.
The main value of automatically implemented properties and initializers is to simplify the syntax around some commonly occurring code.
While it looks like local type inference also is aimed at simplifying syntax, it has a higher purpose. In the next whirlwind session we'll look at anonymous types. I don't want to get ahead of the game, but with an anonymous type the developer does not give the type a name, but the type is known to the compiler. Well, since an anonymous type has no name, we have to use var to declare a variable of such a type. The same also goes for declaring a variable of a generic type that is parameterized by an anonymous type. So var can be both a convenience and a necessity. You'll want to give some thought to the effect of var on the readability of your code and decide when you want to use it. Opinions about when to use and when to eschew var cover the entire spectrum.
Automatically Implemented Properties, section 26.9 in Overview of C# 3.0, MSDN Library
Auto-Implemented Properties, C# Programming Guide, MSDN Library
var, C# Reference, MSDN Library
Implicitly Typed Local Variables, section 26.1 in Overview of C# 3.0, MSDN Library
Implicitly Typed Local Variables, C# Programming Guide, MSDN Library
Implicitly Typed Arrays, C# Programming Guide, MSDN Library
Object and Collection Initializers, section 26.4 in Overview of C# 3.0, MSDN Library
Object and Collection Initializers, C# Programming Guide, MSDN Library
At 8:55, I show a collection initializer that is missing instantiation of the contained type. Here's the code with the error.
The corrected initializer instantiates a new Circle for each item in the collection.
I had it correct in my code (honest!) and must have copied it wrong to the slides. I guess the C# syntax checker in PowerPoint 2007 isn't as good as the one in Visual Studio 2008. (The syntax is written correctly in the next code slide at 9:57.)
As a bonus for enduring the correction, consider that the example illustrates that the syntax supports a collection of types derived from Circle.
public class TexturedCircle : Circle{ public string Texture { get; set; }}var circles = new List<Circle>{ new Circle { Origin = new Point { X = 2, Y = 4 }, Radius = 2 }, new TexturedCircle { Origin = new Point { X = 3, Y = 6 }, Radius = 4, Texture = "bumpy" }};
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Stuart Celarier
Sign In