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 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

Ask A Question

Stats

  • Questions 131k
  • Answers 131k
  • 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 You can't get the name of the item, because it… May 12, 2026 at 6:15 am
  • Editorial Team
    Editorial Team added an answer I do it this way: = link_to image_tag( 'header.jpg'), 'http://www.google.com' May 12, 2026 at 6:15 am
  • Editorial Team
    Editorial Team added an answer SELECT document_id FROM table WHERE tag = 'tag1' OR tag… May 12, 2026 at 6:15 am

Related Questions

I wrote this simple test code, by adapting a piece from a book, to
I have an interesting situation and I'm wondering if there is a better way
I'm trying to brush up on my LINQ by writing some simple extension methods.
I need to implement a simple and efficient XSS Filter in C++ for CppCMS

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.