Radha Manohar
Radha Manohar

Reputation: 419

Object Reference by a constructor?

As we read that, when an object is created for a class like MyClass myClass = new MyClass(); then the instance will be created and the reference pointer will be stored in the stack memory. If the class doesn't have a constructor, then a default constructor will be called, then say the default constructor will return the pointer. Take a case where the class has a non-parametarized constructor, then no reference will be returned from my constructor. Then from where will the pointer for the reference be returned and how will that be created.

Upvotes: 2

Views: 1434

Answers (2)

Brian Rasmussen
Brian Rasmussen

Reputation: 116411

The pointer doesn't come from the constructor. Creation of the object and executing the constructor are two different actions.

The C# language specification (1.6.7.1) states:

An instance constructor is a member that implements the actions required to initialize an instance of a class

Consequently, the constructor doesn't create the object. Instead it initializes the memory associated with the object. In the simple example below, the creation is handled by CORINFO_HELP_NEWFAST, but that's just one of several ways to create an object as pointed out by the post linked below.

To illustrate take a simple program like this.

class Program {
    static void Main(string[] args) {
        var p = new Program();
        Console.ReadLine();
        Console.WriteLine(p.GetType());
    }

    public Program() {
        Console.WriteLine("ctor");
    }
}

Which compiles to the following code.

00760848 55              push    ebp
00760849 8bec            mov     ebp,esp
0076084b 56              push    esi
0076084c b9504d7100      mov     ecx,714D50h (MT: ConsoleApp3.Program)
00760851 e87228faff      call    007030c8 (JitHelp: CORINFO_HELP_NEWSFAST)
00760856 8bf0            mov     esi,eax
00760858 8bce            mov     ecx,esi
0076085a ff15704d7100    call    dword ptr ds:[714D70h] (ConsoleApp3.Program..ctor(),     mdToken: 06000002)
00760860 e82bf32973      call    mscorlib_ni+0xb8fb90 (739ffb90)     (System.Console.ReadLine(), mdToken: 06000b6a)
00760865 8bce            mov     ecx,esi
00760867 e8d0f8ffff      call    0076013c (System.Object.GetType(), mdToken: 0600022e)
0076086c 8bc8            mov     ecx,eax
0076086e e88d1ead72      call    mscorlib_ni+0x3c2700 (73232700)     (System.Console.WriteLine(System.Object), mdToken: 06000b77)
00760873 5e              pop     esi
00760874 5d              pop     ebp
00760875 c3              ret

Notice that the first action is the call to CORINFO_HELP_NEWFAST which creates the object.

This is followed by the call to the constructor which initializes the object, but at that point the object has been allocated and we have a pointer to it. The constructor doesn't create the instance, it initializes it.

More details on this from Vance Morrison here.

Upvotes: 1

Guillaume S.
Guillaume S.

Reputation: 1545

Short answer: all constructors return the object which has been constructed.

If the class doesn't have a constructor, then a default constructor will be called, then say the default constructor will return the pointer.

You are right. However what you call a "default constructor" is actually a parameterless constructor such as public MyClass(). And the compiler does generates one by default if the programmer didn't write one.

Take a case where the class has a non-parametarized constructor, then no reference will be returned from my constructor.

If by "non-parametarized constructor" you mean a constructor with one or more parameters such as public MyClass(int arg), then the compiler will not generate a parameterless constructor by default. However the compiler will ensure you are calling only the constructors that have been defined.

Upvotes: 1

Related Questions