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

  • SEARCH
  • Home
  • 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 7040683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:56:51+00:00 2026-05-28T01:56:51+00:00

If we have the following variable declaration: List<int> list = new List(5); Why does

  • 0

If we have the following variable declaration:

List<int> list = new List(5);

Why does this:

list.insert(2, 3);

fail with the following error:

Index must be within the bounds of the List.

What’s the point of providing the initial size?

  • 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-28T01:56:51+00:00Added an answer on May 28, 2026 at 1:56 am

    All the initial size does is provide a hint to the implementation to have at least a given capacity. It does not create a list filled with N default entries; emphasis mine:

    Initializes a new instance of the List<T> class that is empty and has the specified initial capacity.

    If you continue through the MSDN entry to the Remarks section, you’ll find why this constructor overload is provided (again, emphasis mine):

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

    If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the List<T>.

    In short List<T>.Count is not the same as List<T>.Capacity ("If Count exceeds Capacity while adding elements, the capacity is increased…").

    You receive the exception because the list only logically contains the items you add, changing the capacity does not change the number of items logically stored. If you were to set List<T>.Capacity to less than List<T>.Count we can test this behavior going the other direction:

    Unhandled Exception: System.ArgumentOutOfRangeException: capacity was less than
     the current size.
    Parameter name: value
       at System.Collections.Generic.List`1.set_Capacity(Int32 value)
    

    To perhaps create the behavior you’re looking for:

    public static List<T> CreateDefaultList<T>(int entries)
    {
        return new List<T>(new T[entries]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about following variable declaration. What does that mean? List<string>.Enumerator enumerator
I have the following variable that accepts a file name: var xtr = new
Suppose I am working on a makefile and I have the following variable declaration
I have this table variable declaration followed by a query: DECLARE @CurrentItems TABLE (
Suppose we have the following: void print() { int a; // declaration a =
I have the following code: try { FileStream FS = new FileStream(this.InFile, FileMode.Open, FileAccess.Read);
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
If I have the following nvarchar variable - BTA200, how can I extract just
Let's say I have the following simple table variable: declare @databases table ( DatabaseID
Alright, I have been doing the following (variable names have been changed): FileInputStream fis

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.