You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What we'll see in the console's output?
Most of you're now thinking something like this:
It's kinda simple!
First we assign "b" to "a", so "a" and "b" are now the same
Then we assign "a" (or "b", cause they're the same now) to the "Next" field of object "a" (or "b")
So a.Next == a is true!
And I have some bad news for you, cause it's false :D
If you're surprised, then let me surprise you even more:
a=b;
a.Next =a;// or b
Console.WriteLine(a.Next ==a);// true
Explanation
When we have construction like this:
a=b;
a.x =b;
As a result, we'll get this IL-code:
;store b into aldloc bstloc a ;store b into a.xldloc aldloc b stfld class Foo Foo::X
But if we want to simplify this, and we'll use the following construction (that should mean the same as the example above):
a.x =a=b;
IL will look like this:
ldloc a ;load "a" to the top of the stackldloc b ;load "b" to the top of the stackdup ;load "b" to the top of the stackstloc a ;store "b" to "a"stfld class Foo Foo::X ;store "b" to an object on top of the stack that is no longer "a"!
As we see, the assignment happens to the object we didn't expected (cause we know that assignment goes from right to left).
I found this behaviour kinda unobvious. Especially for those who aren't familiar with the "subtleties" of C#
So I want to suggest introducing compiler warnings for those cases where the left side of an assignment contains a variable, the object inside of which changes somewhere in the right side of the expression, since it wouldn't be very cool to replace a = b; a.x = b; with a similar a.x = a = b; and spend a few sleepless nights in search of a bug :D
The text was updated successfully, but these errors were encountered:
C#
is a beautiful and flexible language that will not allow a beginner to "shoot off his leg"... or it will?Issue
Let's imagine that we have the following class:
Then we want to do something like this:
What we'll see in the console's output?
Most of you're now thinking something like this:
And I have some bad news for you, cause it's
false
:DIf you're surprised, then let me surprise you even more:
Explanation
When we have construction like this:
As a result, we'll get this
IL
-code:But if we want to simplify this, and we'll use the following construction (that should mean the same as the example above):
IL
will look like this:As we see, the assignment happens to the object we didn't expected (cause we know that assignment goes from right to left).
Proposal
I found this behaviour kinda unobvious. Especially for those who aren't familiar with the "subtleties" of
C#
So I want to suggest introducing compiler warnings for those cases where the left side of an assignment contains a variable, the object inside of which changes somewhere in the right side of the expression, since it wouldn't be very cool to replace
a = b; a.x = b;
with a similara.x = a = b;
and spend a few sleepless nights in search of a bug :DThe text was updated successfully, but these errors were encountered: