Skip to content

Instantly share code, notes, and snippets.

@chilversc
Created April 6, 2010 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilversc/357546 to your computer and use it in GitHub Desktop.
Save chilversc/357546 to your computer and use it in GitHub Desktop.
Very odd, but it works
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);
}
}
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