2

I have the following classes:

class X
{
    int x = 100;
}

class Y:X
{
    int y = 100;
}

and the following decompiled code:

class X
{
    int32 x;

    public void X()
    {
       this.x = 100;
       base.Object();
    }
}

class Y:X
{
    int32 y;

    public void Y()
    {
       this.y = 100;
       base.X();
    } 
}

so when I create an instance of Y as in new Y(); I thought that first is created an instance of X type and then an instance of type Y because X is the base class and that base class must be created first than its derived class (Y).

However reading the decompiled code this exists before base and how is possible if base should be created before than this?

Maybe does it exist only one object? And we must always call, first, base class constructor only for initializing purpose?

Practically which is the raison to call first base class constructor if we cannot must create first an instance of that type?

Very confused!

xdevel2000
  • 20,780
  • 41
  • 129
  • 196

2 Answers2

5

Maybe does it exist only one object?

Yes, exactly. A single object is created, which immediately is of the type specified (Y in this case). All fields (across the inheritance hierarchy) are initially set to the default values of their corresponding types. A single object is created, and then initialized using constructors. Starting from the most derived class, the specified constructor is executed:

  • If there's a constructor initializer of the form this(...), that is executed. Otherwise, field initializers are executed.
  • The base class constructor is called; remember that if you don't specify a constructor initializer at all, that's equivalent to base()
  • The body of the constructor is executed

There's no idea of "base being created" or "this being created".

This is described in sections 7.6.10.1 and 10.11 of the C# 5 specification.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I was looking for the language spec that describes the order. Did you find it? (Besides it exists in your head?) – Patrick Hofman Feb 17 '16 at 09:25
  • @PatrickHofman: Just looking for that now :) – Jon Skeet Feb 17 '16 at 09:26
  • Thanks. I just found it. Will put that in an answer. – Patrick Hofman Feb 17 '16 at 09:27
  • Ok, but if 'this' is a reference to the current instance what's 'base'? Is it a reference of what? It's strange to think to only one object if I can do something like base.someMethod() – xdevel2000 Feb 17 '16 at 09:36
  • 1
    Think of memory structure. There is just one place the fields etc. are saved for a class. All fields are joined together in one block (you can have references of course which point somewhere else). All actions, whether on `base` or not, are called on the same object in memory. There is just one. – Patrick Hofman Feb 17 '16 at 09:38
  • @xdevel2000: `base` isn't a valid expression in its own right - it is *only* used in the context of constructor initializers and base access. – Jon Skeet Feb 17 '16 at 09:38
  • we can argue that also 'this' isn't a valid expression in its own right but it's used to refer to the current instance. So, again, 'base' should refer to a pointer in memory where locate fields, methods, etc, of the base class. – xdevel2000 Feb 17 '16 at 09:56
  • 1
    @xdevel2000: No, we can't argue that, because `this` *is* a valid expression in its own right. You can use `object x = this;` but you can't use `object x = base;`. There *is* no object which only has the fields of the base class. See the C# 5 spec sections 7.6.7 and 7.6.8 - they're very signfiicantly different. In the next version of the ECMA C# standard, we've even added a note about this: "Note: Unlike this, base is not an expression in itself. It is a keyword only used in the context of a base-access or a constructor-initializer (§16.12.2). end note." – Jon Skeet Feb 17 '16 at 09:57
  • Ok now is all clear. I noted that if I evaluate into debugger 'this' it returns all fields value (also those of its base class). But if I evaluate 'base' I have an error... At this point should be very interesting to know what the compiler does when it sees base.some_field... – xdevel2000 Feb 17 '16 at 10:04
  • @xdevel2000: It uses the field declared in the base type. This would only be useful if you had two fields with the same name, in the base type and the derived type, and both were accessible. Ick. – Jon Skeet Feb 17 '16 at 10:06
  • In 7.6.8. of the spec I found this: """At binding-time, base-access expressions of the form base.I and base[E] are evaluated exactly as if they were written ((B)this)""". Now is very clear what it meaning base... – xdevel2000 Feb 17 '16 at 10:41
1

The C# programming guide says (in Fields (C# Programming Guide), emphasis mine):

Fields are initialized immediately before the constructor for the object instance is called.

So before anything else is done (even base constructor calls), the field variables are initialized.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325