I have been trying to figure out if there are any differences between these constructors. Assuming there is a Foo() constructor that takes no arguments, are all these constructors going to have the same result?
Example 1
public Foo()
: this()
{
blah;
blah;
blah;
}
Example 2
public Foo()
{
this();
blah;
blah;
blah;
}
Example 3
public Foo()
{
this = new Foo();
blah;
blah;
blah;
}
I would steer clear of assigning to
thisin structs. As you can see from the other answers, the very possibility of it is fairly rarely known (I only know because of some weird situation where it turned up in the spec). Where you’ve got it, it doesn’t do any good – and in other places it’s likely to be mutating the struct, which is not a good idea. Structs should always be immutable 🙂EDIT: Just to make people go “meep!” a little – assigning to
thisisn’t quite the same as just chaining to another constructor, as you can do it in methods too: