Are empty constructors callable in C #?
For an empty constructor like:
internal C()
{
}
Is it being called to create an object? It should only contain a return operation, so in theory it could be dropped, right?
Note. It's clear to me that there is an implicit constructor for every class. However, the intent of this question; if the CLR calls constructors with only operation return
.
source to share
The constructor is always called when an object is created, even if it has no parameters and is empty. If you don't write any constructor, it is implicitly generated by the compiler by default.
This will enable you to write the constructor later without recompiling the calling code.
The object instance always calls the constructor for this reason, except in edge cases such as FormatterServices.GetUninitializedObject
which is the only way I know of to instantiate an object without calling the constructor (it's used for serialization purposes).
newobj
IL opcode (which is used to instantiate a new object) explicitly takes a constructor as an input parameter:
The statement
newobj
creates a new object or a new instance of a value type. Ctor is a metadata token (method ref or methoddef to be marked as a constructor) that specifies the name, class, and signature of the constructor to invoke.The command
newobj
allocates a new instance of the class associated with the ctor and initializes all fields in a new instance up to0
(of the appropriate type) or null references if needed. It then calls the ctor constructor with the given arguments along with the newly instantiated instance. After calling the constructor, the original object reference (type O) is pushed onto the stack.
Thus, the type being created is actually identified by the constructor to call , not by the type token itself, which makes the constructor mandatory.
Since you want to know what the JIT does, here's a parsing of the following line in Release mode ( SomeClass
is a class with an empty default constructor):
var inst = new SomeClass();
64:
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 don't understand the assembly code very well, but the x64 version makes two calls. I am assuming the first is an allocation and the second is a constructor call, but I'm not sure about that (VS won't let me go to those addresses for some reason). This third (indirect) one call
in x86 code is a surprise to me, I don't know what it is for.
source to share
It doesn't matter if you write a constructor or not. Every time you declare an object of any class, the constructor is invalidated.
You can now use this constructor explicitly to do some work.
Or you can just leave it blank or not even write it down in class.
Keeping empty and not writing a constructor is completely analogous.
source to share