C# specification, section 10.4.5.2:
"
A variable initializer for an instance field cannot reference the instance
being created. Thus, it is a compile-time error to reference this in a
variable initializer, as it is a compile-time error for a variable
initializer to reference any instance member through a simple-name.
"
Your code MyCollection list2 = new MyCollection (list); would be compiled as
MyCollection this.list2 = new MyCollection (this.list); and it is not
allowed to use this. in the field initialization.
(in a way, there is not yet a this because it is being constructed).
"cody" <de********@gmx.deha scritto nel messaggio
news:%2****************@TK2MSFTNGP03.phx.gbl...
class Test
{ IList list = new ArrayList();
MyCollection list2 = new MyCollection (list);
}
Leads to this error. I know I could initialize them in the ctor but I'm
asking myself where this restriction come from. Aren't the initializers
called in a well defined order or what is the problem here?