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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:07:41+00:00 2026-05-11T02:07:41+00:00

Given a List<T> in c# is there a way to extend it (within its

  • 0

Given a List<T> in c# is there a way to extend it (within its capacity) and set the new elements to null? I’d like something that works like a memset. I’m not looking for sugar here, I want fast code. I known that in C the operation could be done in something like 1-3 asm ops per entry.

The best solution I’ve found is this:

list.AddRange(Enumerable.Repeat(null, count-list.Count)); 

however that is c# 3.0 (<3.0 is preferred) and might be generating and evaluating an enumerator.

My current code uses:

while(list.Count < lim) list.Add(null); 

so that’s the starting point for time cost.

The motivation for this is that I need to set the n’th element even if it is after the old Count.

  • 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-11T02:07:41+00:00Added an answer on May 11, 2026 at 2:07 am

    The simplest way is probably by creating a temporary array:

    list.AddRange(new T[size - count]); 

    Where size is the required new size, and count is the count of items in the list. However, for relatively large values of size - count, this can have bad performance, since it can cause the list to reallocate multiple times.(*) It also has the disadvantage of allocating an additional temporary array, which, depending on your requirements, may not be acceptable. You could mitigate both issues at the expense of more explicit code, by using the following methods:

    public static class CollectionsUtil {     public static List<T> EnsureSize<T>(this List<T> list, int size)     {         return EnsureSize(list, size, default(T));     }      public static List<T> EnsureSize<T>(this List<T> list, int size, T value)     {         if (list == null) throw new ArgumentNullException('list');         if (size < 0) throw new ArgumentOutOfRangeException('size');          int count = list.Count;         if (count < size)         {             int capacity = list.Capacity;             if (capacity < size)                 list.Capacity = Math.Max(size, capacity * 2);              while (count < size)             {                 list.Add(value);                 ++count;             }         }          return list;     } } 

    The only C# 3.0 here is the use of the ‘this‘ modifier to make them extension methods. Remove the modifier and it will work in C# 2.0.

    Unfortunately, I never compared the performance of the two versions, so I don’t know which one is better.

    Oh, and did you know you could resize an array by calling Array.Resize<T>? I didn’t know that. 🙂

    Update:
    (*) Using list.AddRange(array) will not cause an enumerator to be used. Looking further through Reflector showed that the array will be casted to ICollection<T>, and the Count property will be used so that allocation is done only once.

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

Sidebar

Related Questions

Given that: There seems to be no easy way to get a list of
Given a list of potential ID's is there a quick way using a single
Is there a way using SQL to list all foreign keys for a given
Is there an easy way to list only directories under a given directory in
Given a list like this: num = [1, 2, 3, 4, 5] There are
Given a list of elements like this: <ul> <li class=favourite></li> <li class=favourite></li> <li class=favourite></li>
Given a list say {a, b, c, d} Is there any easier way to
Is there a way to get a list of methods that would be accessible
Given a list A = [1 2 3 4 5 6] Is there any
I have a function that checks for file and directory changes within a given

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.