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 124983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:05:12+00:00 2026-05-11T05:05:12+00:00

.NET offers a generic list container whose performance is almost identical (see Performance of

  • 0

.NET offers a generic list container whose performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization.

Arrays are very easy to initialize with a default value, and by definition they already have certain size:

string[] Ar = new string[10]; 

Which allows one to safely assign random items, say:

Ar[5]='hello'; 

with list things are more tricky. I can see two ways of doing the same initialization, neither of which is what you would call elegant:

List<string> L = new List<string>(10); for (int i=0;i<10;i++) L.Add(null); 

or

string[] Ar = new string[10]; List<string> L = new List<string>(Ar); 

What would be a cleaner way?

EDIT: The answers so far refer to capacity, which is something else than pre-populating a list. For example, on a list just created with a capacity of 10, one cannot do L[2]='somevalue'

EDIT 2: People wonder why I want to use lists this way, as it is not the way they are intended to be used. I can see two reasons:

  1. One could quite convincingly argue that lists are the ‘next generation’ arrays, adding flexibility with almost no penalty. Therefore one should use them by default. I’m pointing out they might not be as easy to initialize.

  2. What I’m currently writing is a base class offering default functionality as part of a bigger framework. In the default functionality I offer, the size of the List is known in advanced and therefore I could have used an array. However, I want to offer any base class the chance to dynamically extend it and therefore I opt for a list.

  • 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. 2026-05-11T05:05:13+00:00Added an answer on May 11, 2026 at 5:05 am

    I can’t say I need this very often – could you give more details as to why you want this? I’d probably put it as a static method in a helper class:

    public static class Lists {     public static List<T> RepeatedDefault<T>(int count)     {         return Repeated(default(T), count);     }      public static List<T> Repeated<T>(T value, int count)     {         List<T> ret = new List<T>(count);         ret.AddRange(Enumerable.Repeat(value, count));         return ret;     } } 

    You could use Enumerable.Repeat(default(T), count).ToList() but that would be inefficient due to buffer resizing.

    Note that if T is a reference type, it will store count copies of the reference passed for the value parameter – so they will all refer to the same object. That may or may not be what you want, depending on your use case.

    EDIT: As noted in comments, you could make Repeated use a loop to populate the list if you wanted to. That would be slightly faster too. Personally I find the code using Repeat more descriptive, and suspect that in the real world the performance difference would be irrelevant, but your mileage may vary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ASP.NET offers 4 types of http handlers - synchronous, asynchronous, generic synchronous (ashx) and
Is there any API SQL Server 2008 offers for .net application to create and
.Net contains a nice control called DocumentViewer . it also offers a subcontrol for
Regarding the ASP.NET Universal Providers (System.Web.Providers), do they automatically offer the retry logic when
reversehttp.net offers little immediate insight into what reversehttp truly is and how this can
The German website nandoo.net offers the possibility to shorten a news article. If you
When I create a ASP.NET MVC web application in Visual Studio 2010 it offers
I need a generic method for preventing XSS attacks in ASP.NET. The approach I
I was wondering if .NET offers any standard functionality for doing a prefix search
In VS.Net 2010 Beta 2: when a team project gets created the wizard offers

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.