Question

If I do the following:

List<GenericClass> listObj = new List<GenericClass>(100);

// Do I need this part too?
for (int i = 0; i < 100; i++)
{
    listObj[i] = new GenericClass();
}

Basically I am asking if the C# compiler will automatically fire the GenericClass constructor for each of the 100 GenericClass objects in the list. I searched in the MSDN documentation as well as here on StackOverflow.

Thanks for any help.

Was it helpful?

Solution

That's not how List works. When you specify a capacity, it's an initial capacity, not the number of items in the list. The list contains no elements until you add them via the Add method. Lists do not have a maximum capacity. And since you're adding objects via the Add method, yes, you would have to new them up first.

In fact, doing what you put in your question would throw an ArgumentOutOfRange exception.

For what you're doing, you'd need to use an array.

var listObj = new List<GenericClass>();
listObj[0] = new GenericClass(); // ArgumentOutOfRange exception

This will work:

for (int i=0;i<100;i++)
{
    listObj.Add(new GenericClass());
}

This is what you were attempting to do:

var arrayObj = new GenericClass[100];
for (int i = 0; i < 100; i++)
{
    arrayObj[i] = new GenericClass();                
}

OTHER TIPS

Yes you do need to create and add each instance to the list. Take a look at the remarks section for this constructor style: http://msdn.microsoft.com/en-us/library/dw8e0z9z.aspx

You are specifying how many elements you expect to put in the list so that the list does not have to resize behind the scenes every time you add a new GenericClass instance to it.

No! It specify the initial capacity.

MSDN article:

The capacity of a List is the number of elements that the List can hold. As elements are added to a List, the capacity is automatically increased as required by reallocating the internal array.

Since you cannot do it directly with List you can use a helper method to have a generator and use the List(IEnumerable collection) overload.

   class Program
    {
        static void Main(string[] args)
        {
            var list = new List<string>
            (
                Generator.New(() => new string('a', 5), 100)
            );

            list.ForEach((x) => Console.WriteLine(x));
        }
    }

    public static class Generator
    {
        public static IEnumerable<T> New<T>(Func<T> generator, int nCount)
        {
            for (int i = 0; i < nCount; i++)
            {
                yield return generator();
            }
        }

        public static IEnumerable<T> New<T>(Func<int,T> generator, int nCount)
        {
            for (int i = 0; i < nCount; i++)
            {
                yield return generator(i);
            }
        }
    }

This does work but it is not so pretty as it could be if List would support this functionality out of the box. The example program will print 100 lines consisting of 5 a characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top