Chris J Cowan

Y U No Use Collection Initializers?

I’ve been tinkering with XNA, Microsoft’s .NET video game framework, off an on since it was released. Recently I’ve been reading a couple XNA 4.0 books, and Riemer’s free XNA tutorials. One thing that constantly baffles me about XNA tutorial material I’ve read is they all use the most painful way to build arrays and List<T> objects.

For example I see a lot of stuff like this:

List<Vector2> path = new List<Vector2>();
path.Add(new Vector2(100, 50));
path.Add(new Vector2(75, 200));
path.Add(new Vector2(35, 100));
path.Add(new Vector2(80, 20));

Since 2008 you’ve been able to do this instead in C#:

List<Vector2> path = new List<Vector2>
{
    new Vector2(100, 50),
    new Vector2(75, 200),
    new Vector2(35, 100),
    new Vector2(80, 20)
};

It’s so much cleaner and readable, especially for tutorial text. It boggles my mind why XNA book and tutorial writers keep using the old way. I understand it’s no walk in the park to write game tutorials – and Riemer’s giving it away for free online – but sill. XNA authors, Y U No Use Collection Initializers?