Using generic lists in code

For best performance it’s usually not a good idea to initialize variables in .NET. When it comes to software maintenance costs, List initialization is a different story, however.

I’ve seen 2 different patterns used when creating lists in C# code recently. One is simply to declare the list and implicitly or explicitly initialize it to null:

public List<SomeObject> MyListProperty { get; set; } // Implicitly null (Ok)
List<SomeObject> MyOtherListProperty = null; // Explicitly null (Ok)

The other is to declare the list and initialize it to an empty list.

List<SomeObject> SomeList = new List<SomeObject>(); // Initialize to empty list (Better)

It’s almost always better to initialize to an empty list unless your application is extremely memory sensitive (see Jon Skeet’s answer here, and understand that List uses an Array to manage data internally).

When you initialize to an empty list, you eliminate null checks prior to using the list, and can immediately iterate using a foreach loop, check the .Count on the list or use methods like .Any() to see if there are items in the list.