Search
XNA MVP

Contact
- Email:
nick@gravelyn.com - MSN Messenger:
nickgravelyn@hotmail.com
Donate
Categories
- .NET
- Artificial Intelligence
- C#
- Challenges
- Coding
- Dream Build Play
- HLSL
- Industry
- Java
- My Picks
- Particle Engine
- Silverlight
- Tile Engine
- Uncategorized
- XNA
- Zune
Latest News
- An Idea of Game Packs
- My Creatures
- Research
- Zune Performance
- I’m a Convert
- Releasing Zune Games Sans Source
- Repeater!
- Too Much Fun
- Teh Internetz in Three Dee
- Embedding Silverlight!
Let’s Talk C# Features, Pt 3
Posted in .NET, C#, XNA on May 13, 2008 at 12:34am.
There are 3 comments.
This will be my last post tonight and coincidentally the last feature of C# 3.0 that will work for all platforms. This compiler trick is the new “implicitly typed local variable”. What does that mean? It means that, for local variables, you no longer have to type the variable type if you also instantiate or initialize it on that same line. The compiler will infer the type from the rest of the line and fill in the type for you. This new feature is done through the ‘var’ keyword. So for instance where you used to write something like this:
string a = "Hello World";
You can now write it like this:
var a = "Hello World";
Before I continue let me clear two things up that some of you might not get right now. First of all this is only for local variables. This means variables that exist only in the scope of a single method. Class level variables, properties, parameters, and method return values cannot be implicitly typed. Secondly this is not weak typing of variables. This is simply a compiler trick so you type less. With the above line the compiler and Intellisense both know of which type is ‘a’. So when you type a followed by a period, you’ll see the same Intellisense help you’ve always seen for a string type. No weak typing; just less typing.
So now you are wondering, “Gee, Nick, this is great and all, but that’s not saving me that much work.” Well we haven’t gotten to the fun stuff yet. Let’s say you have a class called Client and a class called FinanceReport. Let’s now say you want to make a local dictionary using Client for the key and a list of FinanceReports for the value. This is normally what you would see for that declaration:
Dictionary<Client, List<FinanceReports>> clientFinanceReports =
new Dictionary<Client, List<FinanceReports>>();
That looks like an awful long line for something as simple as that. With implicit typing, though, we can reduce the line by a large amount:
var clientFinanceReports =
new Dictionary<Client, List<FinanceReports>>();
Way shorter now. And you can also use var for things like for and foreach loops to make things easier. Let’s say we have that above dictionary and we want to iterate over all the key-value pairs. Previously this is what our loop would look like:
foreach (KeyValuePair<Client, List<FinanceReports>> keyValuePair
in clientFinanceReports)
{
//do something useful
}
And now with implicit typing:
foreach (var keyValuePair in clientFinanceReports)
{
//do something useful
}
How nice is that? You no longer have to write all that generics stuff to loop over the collection saving you lots of typing time.
So remember, everything I’ve shown so far (implicit typing, collection initializers, object initializers, and auto-implemented properties) are all compiler tricks meaning that these will work already on the Zune and Xbox 360 in addition to Windows. The rest of the features I’ll be showing in the next couple days will not work on Zune or Xbox 360 because they do require some runtime enhancements to function properly.
Excellent tips and the efficient coder in me is as giddy as a schoolgirl!
However! The bitter nerd programmer side of me is somewhat afraid that all these shortcuts in will only make it easier to create a harder to maintain codebase.
Let’s just hope the bitter nerd is just spouting his usual nonsensical conspiracies. =)
By Nick on
May 14, 2008 at 8:51 am
I use pretty much all of these with my Zune libraries and it works great. I’d say var makes your code easier to maintain because if you change the type of your collection, the var will automatically have the proper type so as long as the fields/methods/properties you use with it are available on both, you only have to change the declaration and the rest of your code is fine.
By Spodi on
May 24, 2008 at 2:06 am
The ‘var’ is definitely a great addition. At some times I think the verbosity could be needed where you can not easily see what kind of variable it will be from the rest of the line, like with the foreach example, but your dictionary example is something I face waaayyy too often. I think right now I have something like:
Dictionary<string, Dictionary>
The name gets very long, and repeating it twice just makes the line way too large when it is already clear what it will be.
With the foreach() I think it will be very handy for not having to look up what kind of type you are actually enumerating through, but I’m not sure how much I like the idea of relying on placing the cursor over the IEnumerable item to find out what it is. Inside Visual Studio theres no problem, but outside… could get ugly.
Post A Comment
By Magnus on May 13, 2008 at 11:18 am