C# List<GenericClass>(100) Construction Principles

爷,独闯天下 提交于 2020-01-04 05:28:29

问题


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.


回答1:


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();                
}



回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/9147486/c-sharp-listgenericclass100-construction-principles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!