Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't you use 'this' in member initializers? [duplicate]

Tags:

c#

Possible Duplicate:
Cannot use ‘this’ in member initializer?

Any ideas why I get an error if I try to do something like this:

public class Bar {     public Bar(Foo foo)     {     } }  public class Foo {     private Bar _bar = new Bar(this); } 

I get an error saying:

"Cannot use 'this' in member initializer"

but the following works:

public class Foo {     private Bar _bar;      public Foo()     {         _bar = new Bar(this);     } } 

Anyone know the reason behind this? My understanding was that these would compile to the same IL, so am curious as to why one is allowed and the other isn't.

Thanks, Alex

like image 778
AlexC Avatar asked May 25 '11 13:05

AlexC


People also ask

Can you use this pointer in constructor?

Some people feel you should not use the this pointer in a constructor because the object is not fully formed yet. However you can use this in the constructor (in the { body } and even in the initialization list) if you are careful.

What are member initializers in c++?

Member initializer list is the place where non-default initialization of these objects can be specified. For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.

How to initialize a class member in c++?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

What is field initializers in C#?

Fields are initialized immediately before the constructor for the object instance is called. If the constructor assigns the value of a field, it will overwrite any value given during field declaration. For more information, see Using Constructors. A field initializer cannot refer to other instance fields.


2 Answers

I suspect it's to prevent you from using the object before at least the base class constructor has run, ensuring that all the base class members are appropriately initialized. (Variable initializers are executed before the base class constructor, whereas the constructor body is executed after that.)

Will check whether the annotated spec has anything to say about this when I'm next near it...

EDIT: The C# 4 annotated spec doesn't have any explanation. Just (in 10.5.5.2):

A variable initializer for an instance field cannot reference the instance being created.

like image 85
Jon Skeet Avatar answered Sep 18 '22 20:09

Jon Skeet


Field initializers run before base class constructors so this doesn't yet exist. It exists only once the base constructor has finished executing.

17.10.2 Instance variable initializers:

When an instance constructor has no constructor initializer, or it has a constructor initializer of the form base(...), that constructor implicitly performs the initializations specified by the variable-initializers of the instance fields declared in its class. This corresponds to a sequence of assignments that are executed immediately upon entry to the constructor and before the implicit invocation of the direct base class constructor. The variable initializers are executed in the textual order in which they appear in the class declaration.

like image 23
Darin Dimitrov Avatar answered Sep 18 '22 20:09

Darin Dimitrov