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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:58:38+00:00 2026-06-07T04:58:38+00:00

Say I have a C# struct: struct Foo{ int mA; public int A {get

  • 0

Say I have a C# struct:

struct Foo{
    int mA;
    public int A {get {return mA;}}
    int mB;
    public int B {get {return mB;}}

    public Foo(int a, int b)
    {
        mA = a;
        mB = b;
    }
}

And then I create and array of Foo’s:

Foo[] foos = new Foo[10];

What happens when I do this?

foos[1] = new Foo(20, 10);

If Foo was a class, the Foo[] would hold a pointer to a Foo object on the heap, and that pointer would be changed to the new Foo object (the old one being left for recycling).

But since structs are value types, would the new Foo(20, 10) just physically overwrite the same memory location previously held by foos[1]?

  • 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-07T04:58:39+00:00Added an answer on June 7, 2026 at 4:58 am

    In practice the memory associated with the relevant array slot is populated by the values. Given your code a small example shows what goes on. Please see comments inline. This is for a release build.

    static void Main(string[] args)
    {
        Foo[] foos = new Foo[10];
        foos[1] = new Foo(127, 255);
        Console.ReadLine();
    }
    

    The code above is JIT compiled as follows

    // Method setup
    00280050 55              push    ebp
    00280051 8bec            mov     ebp,esp
    00280053 56              push    esi
    
    // Create instance of Foo[]
    00280054 b98a141d00      mov     ecx,1D148Ah
    00280059 ba0a000000      mov     edx,0Ah
    0028005e e8b121f4ff      call    CORINFO_HELP_NEWARR_1_VC (001c2214)
    00280063 8bd0            mov     edx,eax
    
    // Array range check 
    00280065 837a0401        cmp     dword ptr [edx+4],1
    00280069 7624            jbe     
    
    // Assign foos[1] = new Foo(127, 255)  
    0028006b 8d4210          lea     eax,[edx+10h]  <-- load location of foos[1] in eax
    0028006e ba7f000000      mov     edx,7Fh        <-- load 127 in edx
    00280073 beff000000      mov     esi,0FFh       <-- load 255 in esi
    00280078 8910            mov     dword ptr [eax],edx    <-- move the value 127 to foos[1]
    0028007a 897004          mov     dword ptr [eax+4],esi  <-- move the value 255 to foos[1] + offset
    
    // This is just for the Console.ReadLine() part + rest of Main
    0028007d e8d2436305      call    mscorlib_ni!System.Console.get_In() (058b4454)
    00280082 8bc8            mov     ecx,eax
    00280084 8b01            mov     eax,dword ptr [ecx]
    00280086 8b402c          mov     eax,dword ptr [eax+2Ch]
    00280089 ff501c          call    dword ptr [eax+1Ch]
    
    // Epilog
    0028008c 5e              pop     esi
    0028008d 5d              pop     ebp
    0028008e c3              ret
    
    //Exception handling
    0028008f e8f05e7f70      call    clr!JIT_RngChkFail (70a75f84)
    00280094 cc              int     3
    

    So in short, the code loads the constants in registers and then copies values of these registers to the memory associated with the relevant part of the array instance.

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

Sidebar

Related Questions

Let's say I have the following object: struct Foo { int size() { return
Say I have this struct: struct MyStruct { int iID; int iMyNumber; }; Then
Say I have an object: struct Foo { int bar_; Foo(int bar) bar_(bar) {}
Let's say I have a first structure like this: typedef struct { int ivalue;
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have a struct that looks like this (a POD): struct Foo {
Let's say I have a struct struct Foo { void bar () { do_baz(this);
Say I have some windows method and a struct: struct SomeStruct{ int foo; int
Let us say I have: // This is all valid in C++11. struct Foo
Say i have a data structure like this: type Foo struct { Bar []struct

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.