Constructor
why constructor does no have return type?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Feb 5, 2015, 12:40 AM EST
According to the C# 4.0 Language Specification, section 1.6:
Instances of classes are created using the
newoperator, which allocates memory for a new instance, invokes a constructor to initialize the instance, and returns a reference to the instance.It is the
newoperator who is responsible of allocating memory, passing a reference of the newly allocated object to the constructor and then returning a reference to the instance. This mechanism is also explained in section 7.6.10.1:The run-time processing of an object-creation-expression of the form
new T(A), whereTis class-type or a struct-type andAis an optional argument-list, consists of the following steps:· If
Tis a class-type:o A new instance of class
Tis allocated. If there is not enough memory available to allocate the new instance, aSystem.OutOfMemoryExceptionis thrown and no further steps are executed.o All fields of the new instance are initialized to their default values (§5.2).
o The instance constructor is invoked according to the rules of function member invocation (§7.5.4). A reference to the newly allocated instance is automatically passed to the instance constructor and the instance can be accessed from within that constructor as
this.· […]
This would mean that the constructor per se has no return type (
void).Burak SeyhanRank #7,540 · 239 reputationPosted Feb 5, 2015, 1:20 AM EST
Afzaal Ahmad ZeeshanRank #60 · 27.6K reputationPosted Feb 5, 2015, 1:32 AM EST
After the constructor has been called, the object is set to null, and the object (in real) is stored on the heap, but the address of it is stored in the stack for accessing purposes.
VulpesRank #55 · 29.2K reputationPosted Feb 5, 2015, 11:18 AM EST
http://www.c-sharpcorner.com/Forums/Thread/283962/why-constructor-has-not-return-type.aspx