Constructor
why constructor does no have return type?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Feb 5, 2015, 4:18 PM
http://www.c-sharpcorner.com/Forums/Thread/283962/why-constructor-has-not-return-type.aspx
Afzaal Ahmad ZeeshanPosted Feb 5, 2015, 6:32 AM
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.
Burak SeyhanPosted Feb 5, 2015, 6:20 AM
Manish Kumar ChoudharyPosted Feb 5, 2015, 5:40 AM
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).