Reputation: 537
What is the meaning of this following section on constructor initializers?
An instance constructor initializer cannot access the instance being created. Therefore it is a compile-time error to reference
this
in an argument expression of the constructor initializer, as is it a compile-time error for an argument expression to reference any instance member through a simple-name.
Upvotes: 0
Views: 93
Reputation: 263047
This means the instance is still in the process of being created when the constructor initializer runs. Therefore, that initializer cannot access instance members, either through this
or directly:
class Foo
{
private int _bar;
public Foo(int bar)
{
_bar = bar;
}
public Foo() : this(_bar) // Illegal.
{
}
public Foo() : this(this._bar) // Also illegal.
{
}
}
This reasoning applies to both constructor initializers (this()
and base()
).
Upvotes: 8