Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Primitive types (double, int, etc) and Struct differences
    primarykey
    data
    text
    <p>I am confused with structs. </p> <pre><code>class A { double a { get; set; } MyStruct b { get; set; } void Modify() { a = 1.0; // This compiles. b.c = 2.0; // Assuming c is a double. This gives the known error. } } </code></pre> <p>Now why I am able to set the value of the structure a, and not of the structure's field b ? The answer I read is that I have a copy of b and not b's backing field. By the same logic I must have a copy of a, not a, so how it explains that the a property is changed after the permitted assignement </p> <pre><code>a = 1.0; </code></pre> <p>? Thank you.</p> <p><strong>EDIT :</strong><br> Also in order to still use b inside the A class, "filtered" by the set and get methods and not directly by using the backing field _b, something which could look like this: </p> <pre><code>MyStruct _b; MyStruct b { get { return _b; } set { _b=value; } } </code></pre> <p>and then use the field </p> <pre><code>_b </code></pre> <p>, as correctly stated in the answers, I thinked of a silly method to "wrap" the property in an one dimensional array of size 1.</p> <pre><code>MyStruct[] b { get; set; } </code></pre> <p>kind of wrapping the struct with a class. I dont know the computational cost of this, but I preferred it from using the field directly inside the A class. </p> <p><strong>ANSWERED :</strong> Ok, the confusion in this question comes, when you are <strong>used to interpret properties as fields</strong> in your mind. If you interpret properties the correct way -<strong>as the MsIL does</strong>-, all things settle down. All the answers and comments below, are relevant. (Sorry for the bad English.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload