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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:59:01+00:00 2026-05-27T11:59:01+00:00

In my code I sometimes need to create large collections of objects. Please note

  • 0

In my code I sometimes need to create large collections of objects. Please note that I need collections and not arrays, because I might need to add items later.

What is an efficient way to do this?

Let’s assume that objects are quite simple and can be created fast with default parameter-less constructor. Something like this:

class MyObject
{
    private int a;

    public int A
    {
        get { return a; }
        set { a = value; }
    }
}

Sure, I can create my collection like this:

List<MyObject> list = new List<MyObject>(knownNumberOfItems);
for (int i = 0; i < knownNumberOfItems; i++)
    list.Add(new MyObject());

But maybe there is a better way to do the same?

Summary from the discussion:

  • There is no faster way to do this for reference types. You might get some performance improvements using value types.
  • Try allocating more space initially to reduce number of reallocations when new items are added later.
  • 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-27T11:59:01+00:00Added an answer on May 27, 2026 at 11:59 am

    There’s no faster way that I’m familiar with. I’d personally use braces around the body of the for loop, but as a way of creating a list of a given size, populated with distinct references to new objects, that’s about it. The important part is that you specified the size of the list to start with, so those Add calls won’t need to reallocate anything internally.

    If you’re potentially going to add items later on, you may want to give a slightly bigger size, of course.

    It feels unlikely that this is a bottleneck in your system though – and as always, readability should be your primary concern (and isn’t an issue here other than the change I’ve suggested) with performance being constantly measured, but only causing change at a low level when a concern has been validated by measurements.

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

Sidebar

Related Questions

I have a chunk of code where sometimes I need to create a new
I sometimes need to look for information for a special portion of code. When
I'm running code that sometimes yields this: UInt32 current; int left, right; ... //sometimes
Sometimes I have to work on code that moves the computer clock forward. In
Sometimes when I am debugging code in Eclipse it happens that although I can
Sometimes I need to use the same html code in different templates, like: <div
in my WPF application I sometimes need to create a small helper window and
Sometimes I have to test some javascript code, that I use jsbin.com , but
If I have an interface with a few methods. Sometimes I need to create
Why does the following code sometimes causes an Exception with the contents CLIPBRD_E_CANT_OPEN: Clipboard.SetText(str);

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.