Created
April 6, 2010 12:41
-
-
Save chilversc/357546 to your computer and use it in GitHub Desktop.
Very odd, but it works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var f = new Foo (); | |
f.Inc (); | |
f.Inc (); | |
f.X.Dump (); | |
} | |
struct Foo { | |
public readonly int X; | |
public Foo (int x) { X = x; } | |
public void Inc () { | |
this = new Foo (X + 1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
readonly Foo f = new Foo (); | |
void Main() | |
{ | |
// Inc cannot reassign f since its readonly | |
// the assignment is silently ignored. The same | |
// behaviour can be observed when manually trying | |
// to change a readonly field using IL. | |
f.Inc (); | |
f.Inc (); | |
f.X.Dump (); | |
} | |
struct Foo { | |
public readonly int X; | |
public Foo (int x) { X = x; } | |
public void Inc () { | |
this = new Foo (X + 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment