The most important difference is that the this variable for a structured type must be explicitly assigned in the structure's constructor.
A variable of structural type, and this for structures is a variable of structural type, is considered explicitly assigned if each of its fields is explicitly assigned.
How does this affect structures?
This affects the work of designers. Consider the following sample structure declaration.
struct EvenOdd { int x, y; void make_even() { x &= ~0 << 1; } void make_odd() { y |= 1; } public EvenOdd( int x, int y ) { this.x = x; make_even(); this.y = y; make_odd(); } }
For this structure declaration, the compiler will give an error message
Error CS0188 Cannot use this object until all its fields have been assigned.
because at the point of calling the make_even method this is not yet explicitly assigned , because the data member of structure y has not yet been initialized.
After exiting the constructor, the this variable is assumed to be explicitly assigned .
You can make the previous structure constructor valid by first calling the default constructor.
public EvenOdd( int x, int y ) : this() { this.x = x; make_even(); this.y = y; make_odd(); }
In this case, inside the body of the constructor with parameters, the this variable will already be explicitly assigned .
If you change this declaration to a class declaration, then there will be no problems with this , where this no longer a variable, but a value, and this class will be successfully compiled.
class EvenOdd { int x, y; void make_even() { x &= ~0 << 1; } void make_odd() { y |= 1; } public EvenOdd( int x, int y ) { this.x = x; make_even(); this.y = y; make_odd(); } }