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

Ask A Question

Stats

  • Questions 137k
  • Answers 138k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd use one of these: a find_by_sql to get the… May 12, 2026 at 7:27 am
  • Editorial Team
    Editorial Team added an answer This would do it: var table = document.getElementById('gradient-style'); var inputs… May 12, 2026 at 7:27 am
  • Editorial Team
    Editorial Team added an answer That seems perfectly reasonable to me, although I'd use properties… May 12, 2026 at 7:27 am

Related Questions

Most of my programming experience is in a language where there is one collection
I have a self hosted TCP based WCF service. I am now building a
Some APIs, like the WebClient, use the Event-based Async pattern . While this looks
just a quick question: I am a CS undergrad and have only had experience

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.