4

Given an empty constructor such as:

internal C()
{
}

Is it called upon object creation? It should only contain a return operation, so in theory, it could be discarded, right?

Note: It is clear to me, that for each class, an implicit constructor exists. The intend of this question is however; if the CLR calls constructors with exclusively a return operation.

14
  • 2
    Why wouldn't it be called? Jun 1, 2015 at 9:26
  • @LucasTrzesniewski - I don't assume that it shouldn't be called by definiton, but I suspect optimization.
    – toplel32
    Jun 1, 2015 at 9:27
  • Every layer throughout the hierarchy of an object requires a constructor to be called. If you don't do it explicitly, it will be done for you. Jun 1, 2015 at 9:33
  • 3
    The C# compiler will emit the IL to call it, but the JITter may optimize it out. Jun 1, 2015 at 9:34
  • What is the reason behind this question? Are you cleaning code? Are you asking from a C# spec-perspective?
    – flindeberg
    Jun 1, 2015 at 9:41

5 Answers 5

6

A constructor is always called on object instantiation, even if it's parameterless and empty. If you don't write any constructor, a default one is implicitly generated for you by the compiler.

This is to give you the possibility to write a constructor later on, without having to recompile any calling code.

Object instantiation always calls a constructor for this reason, except for edge cases like FormatterServices.GetUninitializedObject which is the only way I know of to instantiate an object without calling a constructor (it's used for serialization purposes).

By the way, the newobj IL opcode (which is used to instantiate a new object), explicitly takes a constructor as an input parameter:

The newobj instruction creates a new object or a new instance of a value type. Ctor is a metadata token (a methodref or methoddef that must be marked as a constructor) that indicates the name, class and signature of the constructor to call.

The newobj instruction allocates a new instance of the class associated with ctor and initializes all the fields in the new instance to 0 (of the proper type) or null references as appropriate. It then calls the constructor ctor with the given arguments along with the newly created instance. After the constructor has been called, the now initialized object reference (type O) is pushed on the stack.

So the type to create is actually identified by the constructor to call, not by the type token itself, which makes a constructor mandatory.


Since you want to know what the JIT does, here's the disassembly of the following line in Release mode (SomeClass is a class with an empty default constructor):

var inst = new SomeClass();

x64:

000007FE95A30093  in          al,dx  
000007FE95A30094  and         byte ptr [rax-73h],cl  
000007FE95A30097  or          eax,0FFEE4014h  
000007FE95A3009C  call        000007FEF5062400  
000007FE95A300A1  mov         rbx,rax  
000007FE95A300A4  call        000007FEEC977A00  

x86:

00320050  push        ebp  
00320051  mov         ebp,esp  
00320053  push        esi  
00320054  mov         ecx,28380Ch  
00320059  call        002720D4  
0032005E  mov         esi,eax  
00320060  call        72EA2578  
00320065  mov         ecx,eax  
00320067  mov         eax,dword ptr [ecx]  
00320069  mov         eax,dword ptr [eax+2Ch]  
0032006C  call        dword ptr [eax+1Ch]  

Ok, I'm not really fluent with assembly code, but the x64 version performs two calls. I suppose the first one is the allocation and the second one is the constructor call, but I'm not sure about that (VS won't let me go to these addresses for some reason). That third (indirected) call in the x86 code is a surprise to me, I don't know what it's for.

4
  • Empty constructors are always called? if i have constructor with parameters and explicit empty constructor, why should it be called from contructor with parameters? dotnetfiddle.net/fuMBYR
    – ASh
    Jun 1, 2015 at 9:45
  • @ASh That's not what I meant, I got my wording wrong, I'll reword this sentence to make it clear. Jun 1, 2015 at 9:46
  • This information is generally reliable, but it doesn't take JIT compilation into account. The JIT compiler could totally bend the rules of virtual instructions.
    – toplel32
    Jun 1, 2015 at 10:05
  • @toplel32 I've added the assembly code to the answer since you seem interested. Let's see if someone more knowledgeable in assembler than me could comment on that. Jun 1, 2015 at 12:35
1

No, the constructor should not be discarded because it's seems like the author of the class intended it to be instantiated only from within its declaring assembly (by marking the constructor as internal).

2
  • That could be right, but does the CLR implementation agree?
    – toplel32
    Jun 1, 2015 at 9:32
  • Check it yourself. You'll not be able to create new instances of the type outside the assembly. But external assembles would still be able to refer to it.
    – haim770
    Jun 1, 2015 at 9:33
0

yes it is called on every object creation as it is the default constructor.You can omit it if you don't have any other parametrised constructor but if you are using other constructors you need to specify default constructor. For more information please see this

0

It doesn't matter if you write a constructor or not. Every time you declare an object of any class will revoke a constructor.

Now you may use this constructor explicitly to get some job done.

Or you may just leave it as blank or even don't write it in the class.

Keeping blank and not writing a constructor are totally similar.

0

No, if you don't supply one, default constructor will be implicitly called. When object is created constructor is passed with nextobjpointer pointing to current object reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.