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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:13:51+00:00 2026-05-18T01:13:51+00:00

Maybe that’s a silly question… But what’s the best (performance and memory wise) way

  • 0

Maybe that’s a silly question… But what’s the best (performance and memory wise) way of creating a constant IEnumerable<TSomeType>…?

If it’s not possible to define “the best” way, which are my options? What is your opinion, do you think there is a most appropriate way of doing that?

For instance:

  • var enumerable = (IEnumerable<TSomeType>) new List<TSomeType> { Value1, Value2, Value3 };
  • var enumerable = (IEnumerable<TSomeType>) new TSomeType[] { Value1, Value2, Value3 };
  • (some other option; for instance a Linq Select).

Please consider that memory and performance are an issue here – we’re talking about a really constrained environment (a small device with .NET installed).

Thanks in advance.

  • 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-18T01:13:52+00:00Added an answer on May 18, 2026 at 1:13 am

    Well, neither List<T> nor arrays are immutable, so they’re out if you’re really after immutability – the caller could cast the result and then modify it.

    You could create a List<T> and wrap that in a ReadOnlyCollection<T>. If nothing has a reference to the original list any more, then it’s effectively immutable, barring reflection.

    If you don’t actually care about immutability – i.e. if you trust all the code not to mess with it – then an array is going to be the most performant approach, almost certainly. There are various CLR-level optimizations which make them work blazingly fast. However, in that case I wouldn’t cast to IEnumerable<T> – I’d just expose it as an array. That will make it faster to iterate over than if the compiler has to call GetEnumerator().

    If the C# compiler sees a foreach statement over an array, it generates calls to go straight to the indexer and use the Length property… and then the CLR will also be able to remove bounds checking, spotting the pattern.

    Likewise if you decide to go with List<T>, leave it as a List<T> – that way you’ll get to use List<T>.Enumerator – which is a struct – directly, without boxing.

    EDIT: Steve Megson brings up the point of using LINQ for this. Actually, you can probably do better than that, because once you’ve got the enumerator of the underlying list, you can give that back to the caller safely, at least for all collections I’m aware of. So you could have:

    public class ProtectedEnumerable<T> : IEnumerable<T>
    {
        private readonly IEnumerable<T> collection;
    
        public ProtectedEnumerable(IEnumerable<T> collection)
        {
            this.collection = collection;
        }
    
        public IEnumerator<T> GetEnumerator()
        {
            return collection.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

    That means there’s only a tiny hit when iterating – just the single delegated call to GetEnumerator(). Compare that with using Enumerable.Select, which will need to take an extra delegation hit on every call to MoveNext() (as well as the no-op projection).

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

Sidebar

Related Questions

Maybe that's a somewhat uncommon question but here we go. Here's the code $userName=(Google);
Maybe that's silly, but I'm confused again when trying to find appropriate names for
Does anyone know if Python's shelve module uses memory-mapped IO? Maybe that question is
I realised this might be relatively niche, but maybe that's why this is good
For desktop application that is. This is just a general question that maybe only
I have a little dilemma that maybe you can help me sort out. I've
This is one of those things, that maybe so simple I'll never find it
I have a table that contains maybe 10k to 100k rows and I need
Maybe there are VB.net and other language that related with .Net framework. When I
Assume that the core project has a base entity and every plugin maybe extends

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.