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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:54:08+00:00 2026-06-17T11:54:08+00:00

I have an array of structs. Does foreach operator make a copy of each

  • 0

I have an array of structs. Does foreach operator make a copy of each element when iterating thru an array? As far as I understand foreach is just syntactic sugar under the hood converted to for. So it appears the answer is no but I’d love to have some confirmation.

PS: it appears someone should have already asked that but I can’t easily find anything. So please vote as a dup with provided reference.

  • 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-17T11:54:09+00:00Added an answer on June 17, 2026 at 11:54 am

    Yes, copies of the value type instances will be made. When iterating over an array, foreach will indeed use array accesses instead of using an enumerator, but the value in each array slot is still copied.

    This code:

    struct AStruct
    {
        public string a;
        public int b;
    
        static void Main()
        {
            var structs = new AStruct[10];
    
            foreach (var x in structs) {
                Console.WriteLine(x);
            }
        }
    }
    

    Generates the following IL for the Main() method:

    .method private static hidebysig
           default void Main ()  cil managed
    {
        .entrypoint
        .maxstack 4
        .locals init (
                valuetype AStruct[]     V_0,
                valuetype AStruct[]     V_1,
                int32   V_2,
                valuetype AStruct       V_3)
        IL_0000:  ldc.i4.s 0x0a
        IL_0002:  newarr AStruct
        IL_0007:  stloc.0
        IL_0008:  ldloc.0
        IL_0009:  stloc.1
        IL_000a:  ldc.i4.0
        IL_000b:  stloc.2
        IL_000c:  br IL_002d
    
        IL_0011:  ldloc.1
        IL_0012:  ldloc.2
        IL_0013:  ldelema AStruct
        IL_0018:  ldobj AStruct
        IL_001d:  stloc.3
        IL_001e:  ldloc.3
        IL_001f:  box AStruct
        IL_0024:  call void class [mscorlib]System.Console::WriteLine(object)
        IL_0029:  ldloc.2
        IL_002a:  ldc.i4.1
        IL_002b:  add
        IL_002c:  stloc.2
        IL_002d:  ldloc.2
        IL_002e:  ldloc.1
        IL_002f:  ldlen
        IL_0030:  conv.i4
        IL_0031:  blt IL_0011
    
        IL_0036:  ret
    } // end of method AStruct::Main
    

    Note the instructions IL_0013 through IL_001d. The entire value at each array slot is pushed onto the stack and stored in the local V_3 (the x iteration variable).

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

Sidebar

Related Questions

I have an array of structs and I intend to pass each element of
I have a small array of structs, each struct has three fields, all strings.
How does one malloc an array of structs correctly if each struct contains an
I have an array of structs. Actually, it is a 2d-array but an unusual
I have an array of structs and one of the fields in the struct
I have an array of objects that have QTTime structs as attributes. The objects
I have a (150x150)2d array of structs that i'd like to sort in a
I have data represented in CF as an array of structs e.g.: var foo
Is it possible to have an array of elements (structs) where the values vary
Someone please help me understand why this binding does not work... I have a

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.