none
Why C# Compiler is providing return type to its implemented constructor i.e., .ctor()

    السؤال

  • Hi All,

    Until now i was knowing that constructors do not have return type ,not even Void.

    But, hey have you seen in output of executable assembly through ILDASM ?

    View->MetaInfo->Show!

    In that file i saw this:

    Method #2 (06000002) 
    	-------------------------------------------------------
    		MethodName: .ctor (06000002)
    		Flags   : [Public] [HideBySig] [ReuseSlot] [SpecialName] [RTSpecialName] [.ctor] (00001886)
    		RVA    : 0x0000206b
    		ImplFlags : [IL] [Managed] (00000000)
    		CallCnvntn: [DEFAULT]
    		hasThis 
    		ReturnType: Void
    		No arguments.
    
    

    You ca see ReturnType:Void !!!

    So,please explain me why the compiler is providing this return type ?And why we were not told earlier that constructors too have return type though it is given by compiler.

    Can the return type be anything different from Void like Int32 or Boolean or String like that ?

    But basically i want to know why compiler is providing this return type ? And why were we not allowed to give in source code ?


    Thanks.
    04/صفر/1432 12:50 م

الإجابات

  • Different languages, different syntax. One designed for human and one designed for machine. Do you want to type an extra "void" for every single constructor in the source code?

    Like other instance methods, non-static constructors are called with an object reference as a hidden argument. There is no need to give the same reference back, the caller already has it.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • تم وضع علامة كإجابة بواسطة Cookie Luo 13/صفر/1432 01:47 ص
    04/صفر/1432 07:42 م
  • Again, you are mixing two languages. MSIL is written for machines. It is not designed for best code readability.

    What you are talking about is C#'s operator new which allocates memory for an object and call the constructor. It has nothing to do with how the constructor is implemented.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • تم وضع علامة كإجابة بواسطة Cookie Luo 13/صفر/1432 01:47 ص
    05/صفر/1432 07:00 ص

جميع الردود

  • But why constructor have Void return type ? According to my present understanding Constructors should have return type that should match the class in which that constructor is residing since that constructor is creating the actual instance(object ) of the type and that constructor is returning the reference(address) to the reference variable (to the left side of new operator).

    Really the situation is even more dark for me. Really i am confused about constructor's behavior.

    Please help!


    Thanks.
    04/صفر/1432 01:06 م
  • Different languages, different syntax. One designed for human and one designed for machine. Do you want to type an extra "void" for every single constructor in the source code?

    Like other instance methods, non-static constructors are called with an object reference as a hidden argument. There is no need to give the same reference back, the caller already has it.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • تم وضع علامة كإجابة بواسطة Cookie Luo 13/صفر/1432 01:47 ص
    04/صفر/1432 07:42 م
  • Hi Sheng Jiang,

    "constructors are called with an object reference as a hidden argument".

    what is hidden argument ? For this statement:

    TypeA obj=new TypeA();

    Constructor TypeA() creates the instance and returns the address of that created instance, since the constructor is returning whose type is TypeA (or any base type) , so i thought constructors should have a return type and that type should be the class itself for which it is actually creating the instance.

    But why it is Void ?


    Thanks.
    05/صفر/1432 06:51 ص
  • Again, you are mixing two languages. MSIL is written for machines. It is not designed for best code readability.

    What you are talking about is C#'s operator new which allocates memory for an object and call the constructor. It has nothing to do with how the constructor is implemented.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • تم وضع علامة كإجابة بواسطة Cookie Luo 13/صفر/1432 01:47 ص
    05/صفر/1432 07:00 ص
  • That's implementation detail of instance methods at the machine code level. Instructions for instance functions are stored in memory and the address of the instance is pushed to the stack like other parameters prior to calling the function.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    05/صفر/1432 03:54 م
  • You can just write a program, run ildasm, and add a line that creates a new string, then run ildasm, compare the results.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    06/صفر/1432 03:40 م