yeomandev
yeomandev

Reputation: 11806

In C# how to create a List<T> with a default count of zero?

Rookie question:

I have been experiencing a minor bug in my mvc2 application. I was able to trace it back to this code:

List<Stream2FieldTypes> Stream2FieldTypes = new List<Stream2FieldTypes>();
foreach (var item in stream.Stream2FieldTypes) 
{ 
   Stream2FieldTypes.Add(item); 
}

The problem that I am experiencing is that when I instatiate the new list, it has a count of one. I'm thinking that this is probably due to my using the constructor. So I tried this:

List<Stream2FieldTypes> Stream2FieldTypes;
foreach (var item in stream.Stream2FieldTypes) 
{ 
   Stream2FieldTypes.Add(item); 
}

But, of course this will not compile because of an error on Stream2FieldTypes.Add(item);. Is there a way that I can create a List<Stream2FieldTypes> and make sure that the count is zero?

Upvotes: 2

Views: 3325

Answers (5)

Andy Xufuris
Andy Xufuris

Reputation: 708

It looks to me that you have your constructor set up incorrectly. I may be wrong but instead of List<Stream2FieldTypes> Stream2FieldTypes = new List<Stream2FieldTypes>(); you should be naming it different than the type you are using? List<Stream2FieldTypes> SomethingElse = new List<Stream2FieldTypes>();

Try that it should work.

Upvotes: 0

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

The constructor:

List<Stream2FieldTypes> Stream2FieldTypes = new List<Stream2FieldTypes>(0);

will create a list with a default capacity of zero.

ETA: Though, looking at Reflector, it seems that the static and default constructors also create the list with a default capacity of zero. So your code as it stands should create a list with no elements and no reserved capacity. Should be more performant than the explicit constructor.

Upvotes: 3

Stan R.
Stan R.

Reputation: 16065

This seems like a multi-threading issue, are you sure that this is a thread safe method and another thread didn't already add an item to this list?

We need to see this method in a bigger context of your code.

Upvotes: 0

Ana Betts
Ana Betts

Reputation: 74682

Also, if you use IEnumerable, you can do some nice tricks:

public void processTheList(List<string> someList = null)
{
    // Instead of special-casing null, make your code cleaner
    var list = someList ?? Enumerable.Empty<string>();

    // Now we can always assume list is a valid IEnumerable
    foreach(string item in list) { /* ... */ }
}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039100

The problem that I am experiencing is that when I instatiate the new list, it has a length of one

No, that's totally impossible. Your problem is somewhere else and unrelated to the number of elements of a newly instantiated list.

List<Stream2FieldTypes> Stream2FieldTypes = new List<Stream2FieldTypes>();

Stream2FieldTypes.Count will be 0 at this point no matter what you do (assuming of course single threaded sequential access but List<T> is not thread-safe anyways so it's a safe assumption :-)).

Upvotes: 14

Related Questions