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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:55:51+00:00 2026-06-13T19:55:51+00:00

To save the memory, I defined some record-like structs and stored their instances in

  • 0

To save the memory, I defined some record-like structs and stored their instances in arrays, as the following code shows:

struct Foo
{
    readonly char _bar;
    readonly int _baz;
    // other probable fields ...
    public char Bar{get{return _bar;}}
    public int Baz{get{return _baz;}}

    public Foo(char bar, int baz){_bar = bar; _baz = baz;}
}

static void Main ()
{
    Foo[] arr = new Foo[1000000];
    int baz;
    for(var i = 1000000; i-- > 0;){ arr[i] = new Foo('x',42); }
    for(var i = 1000000; i-- > 0;)
    { 
        baz = arr[i].Baz; //Will the Foo obj resides at arr[i] be copied?
    }
}

I know the copying won’t happen if the stuff above was implemented in C/C++, yet I’m not sure about C#, what if I replace the Foo[] with an List<Foo>? C# doesn’t have a mechanism to return a reference, so theoretically the indexer would return either a pointer(4 bytes) for reference type or the whole copy for value type, if the return value optimization was not involved. So, could anyone tell me, whether this sort of ridiculous copying is guaranteed to be avoided by .NET/MONO jit ?

  • 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-06-13T19:55:53+00:00Added an answer on June 13, 2026 at 7:55 pm

    No, the value isn’t copied – arr[i] is classified as a variable, so Baz will be fetched “in place”. It took me a while to work out how you could validate that, but it is just about possible, by making the property change the value in the array…

    using System;
    
    public struct Foo
    {
        private int field;
    
        public int Value
        {
            get
            {
                Test.array[0].field = 10;
                return field;
            }
        }
    }    
    
    public class Test
    {
        public static Foo[] array = new Foo[1];
    
        static void Main()
        {
            Console.WriteLine(array[0].Value); // Prints 10
        }
    }
    

    Note that your comment about “the Foo obj” is somewhat misleading in C# – there isn’t an object here; there’s just a value of type Foo.

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

Sidebar

Related Questions

In .Net, you can generate byte code in memory, and presumably save the resulting
Is it posible to dump/save an in memory object so that the dump contains
How can I save QML Image into phone memory ?? also if saving the
I have an object caching class that can save an object in memory, the
My application generates loads of images and in order to save memory, I write
I was following the answer for this question ( How To Save Data For
When writing a custom string class that stores UTF-8 internally (to save memory) rather
Is the dependency property system in WPF the Flyweight Pattern applied to save memory
I'm looking at reducing the memory consumption of a table like collection object. Given
I know that solr uses normalization of floats to save memory and speed up

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.