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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:30:55+00:00 2026-06-14T15:30:55+00:00

The question raised in my mind after Is it possible to access a reference

  • 0

The question raised in my mind after Is it possible to access a reference of a struct from a List to make changes? thread by reza.

So, consider the following struct and interface (definetely not very useful, but just to show the issue):

public interface IChangeStruct
{
    int Value { get; }
    void Change(int value);
}

public struct MyStruct : IChangeStruct
{
    int value;

    public MyStruct(int _value)
    {
        value = _value;
    }

    public int Value
    {
        get
        {
            return value;
        }
    }

    public void Change(int value)
    {
        this.value = value;
    }
}

MyStruct implements IChangeStruct, so we can change a boxed copy of it right in the heap without unboxing and replacing with a new one. This can be demostrated with the following code:

MyStruct[] l1 = new MyStruct[]
{
    new MyStruct(0)
};

Console.WriteLine(l1[0].Value); //0
l1[0].Change(10);
Console.WriteLine(l1[0].Value); //10

Now, let’s change array to List<T>, i.e.:

List<MyStruct> l2 = new List<MyStruct>
{
    new MyStruct(0)
};

Console.WriteLine(l2[0].Value); //0
l2[0].Change(10);
Console.WriteLine(l2[0].Value); //also 0

As far as I understood, in the first case l1[0] returned the referense to the boxed struct, while in the second – it was smth else.

I also tried to disassemble this and found:

1) For MyStruct[]:

IL_0030:  ldelema    Utils.MyStruct
IL_0035:  ldc.i4.s   10
IL_0037:  call       instance void Utils.MyStruct::Change(int32)

2) For List<MyStruct>:

 IL_007c:  callvirt   instance !0 class [mscorlib]System.Collections.Generic.List`1<valuetype Utils.MyStruct>::get_Item(int32)
 IL_0081:  stloc.s    CS$0$0001
 IL_0083:  ldloca.s   CS$0$0001
 IL_0085:  ldc.i4.s   10
 IL_0087:  call       instance void Utils.MyStruct::Change(int32)

But I appeared to be not ready to interpret it well.

So, what did the List<T> return? Or how do array and List<T> return elements by index? Or is this only the case with value types and has nothing to do with reference types?

P.S.: I do understand that one must not change a value type instance, but the described issue made me understand, I never realized how List<T> and array work.

  • 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-14T15:30:57+00:00Added an answer on June 14, 2026 at 3:30 pm

    .Net can address array elements in-place, using the ldelema instruction (load address of array element).

    This allows you to operate directly on array elements without copying them. (this is also why you can pass an array element as a ref or out parameter)

    List<T> has no such capability. Instead, list[i] is just syntactic sugar for list.get_Item(i), which is a normal method call that returns a copy of the struct.

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

Sidebar

Related Questions

The question raised from the question here . In order to have search engines
This question follows on from a previous question, that has raised a further issue.
Recently a question raised in my group. I have a sections list in database.it
A question was raised in a discussion I had around whether an interface method
I had a question answered which raised another one, why following does not work?
This question is an extension to the one raised here: Using factory_girl in Rails
I ask this question following the issue I raised here . The point is
A coworker's typo when calling a subref raised this strange syntax question. If I
See the title of this question. I want to play with the exception raised
Original question has been answered. Update addresses related question raised in comments. Original post:

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.