F
F
footballer2019-03-22 17:52:47
.NET
footballer, 2019-03-22 17:52:47

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

1 answer(s)
F
freeExec, 2019-03-22
@footballer

Uh X++=> X = X + 1The value of the copy then changes, but then it is written back.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question