Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7435379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:03:51+00:00 2026-05-29T10:03:51+00:00

If I do the following: List<GenericClass> listObj = new List<GenericClass>(100); // Do I need

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:03:52+00:00Added an answer on May 29, 2026 at 10:03 am

    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();                
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following List<int> collection and I need to find the highest integer
Say I have the following list: List<int> integerList = new List<int> { 1, 10,
Assume you are doing something like the following List<string> myitems = new List<string> {
I have the following list: <ul> <li onclick=populateComment(..this.)>very good </li> <li onclick=populateComment(..this.)>not so good</li>
I have the following list: var list = new List<string>(); // list.Add(foo); list.Add(baa); do
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
Given the following List: List<String> list = new ArrayList<String>(); list.add(s1); list.add(s2); list.add(null); list.add(s3); list.add(null);
Imagine the following list: List<List<List<String>>> listRoot = new List<List<List<String>>>(); I want to count the
I have the following code: List<byte[]> list1 = new List<byte[]>(); list1.Add(new byte[] { 0x41,
Question The following list of statements will quickly introduce my question: An Interface called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.