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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:47:51+00:00 2026-06-15T17:47:51+00:00

In the following code, a struct is obtained from an array and from a

  • 0

In the following code, a struct is obtained from an array and from a list. When getting the item by index, the array appears to do it by reference whereas the list appears to do it by value. Can someone explain the reasoning behind this?

struct FloatPoint {
    public FloatPoint (float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public float x, y, z;
}

class Test {

    public static int Main (string[] args) {
        FloatPoint[] points1 = { new FloatPoint(1, 2, 3) };

        var points2 = new System.Collections.Generic.List<FloatPoint>();
        points2.Add(new FloatPoint(1, 2, 3));

        points1[0].x = 0; // not an error
        points2[0].x = 0; // compile error

        return 0;
    }
}

Changing the struct definition to a class makes both compile.

  • 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-15T17:47:52+00:00Added an answer on June 15, 2026 at 5:47 pm

    When you get a struct, it is always by value. The structure will be copied, you don’t get a reference to it.

    The difference is that you can access the sctruct directly in the array, but not in the list. When you change the property in the struct in the array, you access the property directly, but to do the same with a list you have to get the struct, set the property, then store the struct back in the list:

    FloatPoint f = points2[0];
    f.x = 0;
    points2[0] = f;
    

    Earlier versions of the compiler would let you write the code that you have, but for a list it would generate code similar to this:

    FloatPoint f = points2[0];
    f.x = 0;
    

    I.e. it would read the struct, change it, and silently throw the changed struct away. The compiler was changed to give an error in that case.

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

Sidebar

Related Questions

Consider the following code: struct TrainingExample { array<double, N> input; array<double, M> output; };
I am not clear with following code struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy); this can
Given the following code: struct Item { std::string name; int someInt; string someString; Item(const
In the following code: struct Foo { Foo(int x=0); }; Does the constructor count
Consider the following code: struct Foo { Foo operator+(const Foo &rhs) const; // notice
Consider the following code: struct Calc { Calc(const Arg1 & arg1, const Arg2 &
I use the following code: struct WorkData { std::string name; std::function<void(std::string)> Callback; WorkData(){}; WorkData(const
Say I have the following code: struct date { int day; int month; int
Let's say I have the following code: struct mytype { ~mytype() { /* do
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =

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.