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 2
Posted in .NET, C#, XNA on May 13, 2008 at 12:14am.
There are no comments.
I’m on a roll with using the new features in my own code so let’s keep going with more of the compiler tricks you can already use on all the platforms. I want to hit on two closely related topics in this post: object initializers and collection initializers.
Previously in C# if you wanted to create a list with some values you had instantiate the list and run the Add method a bunch with values like this:
Listvalues = new List (); values.Add(1); values.Add(2); values.Add(3); values.Add(4); values.Add(5);
Well this sucks. You wind up typing a lot of the same code over and over again. In come collection initializers to the rescue. Starting with C# 3.0 you can now instantiate and populate the list in a single statement like this:
Listvalues = new List () { 1, 2, 3, 4, 5 };
How’s that for efficiency? All on one line, no less. And like I said above this is a compiler trick that expands out to the same as above so it works on Zune and Xbox 360. Not only that but it works on most, if not all, of the built in collection types in .NET including List, Queue, Dictionary, etc.
The second topic is the object initializers. Let’s take a look at a simple structure that holds three values:
public struct SomeStruct
{
public int Value1 { get; set; }
public float Value2 { get; set; }
public bool Value3 { get; set; }
}
Let’s say you want to be able to instantiate an object of this type using any combination of setting those values. To do this you used to have to write out every combination as a constructor. For this three value struct you get this:
public struct SomeStruct
{
public int Value1 { get; set; }
public float Value2 { get; set; }
public bool Value3 { get; set; }
public SomeStruct(int value1) { }
public SomeStruct(int value1, float value2) { }
public SomeStruct(int value1, bool value3) { }
public SomeStruct(int value1, float value2, bool value3) { }
public SomeStruct(float value2) { }
public SomeStruct(float value2, bool value3) { }
public SomeStruct(bool value3) { }
}
That’s a pain to do. Then let’s imagine it for a structure with ten or twenty values. Not very fun. This problem is easily solved with object initializers. Object initializers look a lot like collection initializers, but they let you set the values of any public properties on a type while instantiating. So now we can easily instantiate the above structure in any way we want without having to write these constructors:
SomeStruct a = new SomeStruct();
SomeStruct b = new SomeStruct() { Value2 = 1.5f };
SomeStruct c = new SomeStruct()
{
Value1 = 10,
Value2 = 1.5f,
Value3 = false
};
Now no matter how many values you have in the structure you can always feel like you have a constructor that sets all of them.
Then we can now take these concepts and combine them so that we can create a list of complex types all in a single statement:
Listvalues = new List () { new SomeStruct() { Value1 = 1 }, new SomeStruct() { Value1 = 2, Value2 = 1.5f }, new SomeStruct() { Value1 = 3, Value3 = false }, new SomeStruct() { Value1 = 4, Value2 = 1.5f, Value3 = true }, };
Pretty awesome stuff, huh? And once more, these are all compiler tricks in C# 3.0 meaning you can get started using them on your Windows, Xbox 360, and Zune games.
Post A Comment