Answer the question
In order to leave comments, you need to log in
How do properties work with value types - why so?
The question is caused by this article https://m.habr.com/ru/post/124404/ Fiddle link https://dotnetfiddle.net/uFhUvl
In the
fiddle
code, the IncrementX method accesses the X autoproperty of the int value type and increments it, those. purely logically, at first the getter should return not a reference to the field, but a copy of the field, and then the increment operation changes the value of the copy. As a result, at least the result should have been 5, not 6, and as a maximum, the
"Cannot modify the return value of 'something here' because it is not a variable" exception should have fallen.
But in fact, the result is 6, not 5, and there are no exceptions. Can anyone explain?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question