none
constructor return type RRS feed

  • Question

  • constructor don't have return type.so can we assert the following statement:
     the implicit return type of a class’ constructor is the class type itself.

    regards, sushma rai http://in.linkedin.com/pub/sushma-rai/28/bb0/988
    Monday, September 5, 2011 6:51 AM

Answers

  • Hi do not fully agree to that. The constructor is called to initialize the new instance of the type given. But the memory must eb allocated and so on and this is not done by the constructor. So I would still say, that the constructor is not returning anything. It is only initializing the newly created instance.

    So at new DataSet() the new operator is returning an object of type DataSet - not the constructor!

    With kind regards,

    Konrad

     

    • Proposed as answer by Louis.fr Monday, September 5, 2011 4:42 PM
    • Marked as answer by Leo Liu - MSFT Monday, September 12, 2011 5:24 AM
    Monday, September 5, 2011 7:39 AM
  • First of all constructor is not a method. Without new operator constructor is of no use. If you do something like new MyClass(); - Here new is a operator that operates on one operand. If the operand is a constructor, it allocates memory and returns object of type MyClass.

    In short, The constructor along with new operator returns an object  of the type specified by the constructor.


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Edited by Adavesh Monday, September 5, 2011 7:51 AM
    • Marked as answer by Leo Liu - MSFT Monday, September 12, 2011 5:25 AM
    Monday, September 5, 2011 7:51 AM
  • Hi sushma,

    To quote the spec, "An instance constructor is a member that implements the actions required to initialize an instance of a class." That's actually a pretty good description on its own. A constructor is invoked when you use the "new" operator, or use the various methods of reflection to create an instance of a class.

    A constructor looks very much like a method, with a name which is the same as the name of the class, but without a return type.
    You can test with a constructor with your defined class, append this line in the end of the constructor:
    return;
    Is any error thrown?
    Change this line to:
    return this;
    Or return something else, is any error thrown now? If so, you can find some useful info in the error.

    Now let take a look at this:
    new SomeClass();
    Here our target is to explain the new keyword, so we just suppose that we are instantiating a class. We know that a reference to an instance of the SomeClass type is got from the above line, now let's have a look on at what the new keyword does here:
    The following paragraphs are quoted from the book "CLR via C# 3rd Edition":
    ----------------------------
    Here’s what the new operator does:
    1. It calculates the number of bytes required by all instance fields defined in the type and all of its base types up to and including System.Object (which defines no instance fields of its own). Every object on the heap requires some additional members—called the type object pointer and the sync block index—used by the CLR to manage the object. The bytes for these additional members are added to the size of the object.
    2. It allocates memory for the object by allocating the number of bytes required for the specified type from the managed heap; all of these bytes are then set to zero (0).
    3. It initializes the object’s type object pointer and sync block index members.
    4. The type’s instance constructor is called, passing it any arguments (the string "ConstructorParam1" in the preceding example) specified in the call to new. Most compilers automatically emit code in a constructor to call a base class’s constructor. Each constructor is responsible for initializing the instance fields defined by the type whose constructor is being called. Eventually, System.Object’s constructor is called, and this constructor method does nothing but return. You can verify this by using ILDasm.exe to load MSCorLib.dll and examine System.Object’s constructor method.
    After new has performed all of these operations, it returns a reference (or pointer) to the newly created object. In the preceding code example, this reference is saved in the variable e, which is of type Employee.
    ----------------------------

    Have a nice day,


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Leo Liu - MSFT Monday, September 12, 2011 5:25 AM
    Wednesday, September 7, 2011 4:11 AM

All replies

  • To assert you need to throw the exception from the constructor.

    Visit the below link as well:

    http://stackoverflow.com/questions/5586186/c-type-getconstructor-returns-null

     


    Cheers!!!

    Vatsa

    www.objectiveprogramming.com

     

    • Proposed as answer by vatsa_mitr Monday, September 5, 2011 7:12 AM
    • Edited by vatsa_mitr Monday, September 5, 2011 7:12 AM
    Monday, September 5, 2011 7:12 AM
  • Yes

    Because New DataSet() will return an Object of type DataSet


    --------------------------------------------------------

    Surender Singh Bhadauria

     

    Monday, September 5, 2011 7:13 AM
  • Hi do not fully agree to that. The constructor is called to initialize the new instance of the type given. But the memory must eb allocated and so on and this is not done by the constructor. So I would still say, that the constructor is not returning anything. It is only initializing the newly created instance.

    So at new DataSet() the new operator is returning an object of type DataSet - not the constructor!

    With kind regards,

    Konrad

     

    • Proposed as answer by Louis.fr Monday, September 5, 2011 4:42 PM
    • Marked as answer by Leo Liu - MSFT Monday, September 12, 2011 5:24 AM
    Monday, September 5, 2011 7:39 AM
  • First of all constructor is not a method. Without new operator constructor is of no use. If you do something like new MyClass(); - Here new is a operator that operates on one operand. If the operand is a constructor, it allocates memory and returns object of type MyClass.

    In short, The constructor along with new operator returns an object  of the type specified by the constructor.


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Edited by Adavesh Monday, September 5, 2011 7:51 AM
    • Marked as answer by Leo Liu - MSFT Monday, September 12, 2011 5:25 AM
    Monday, September 5, 2011 7:51 AM
  • Hi,

    The Operand is not a constructor. It is a type which can be followed by an argument list in brackets. This is documented inside the C# Language Specification (7.6.10.1 describes this. The 7.6.10 covers the new operand and you can find a lot of documentation in there.) And that it is not a "constructor call" is visible as soon as you have an object or collection Initializer and no arguments: The brackets can be left out then!

    Just the fact, that the Type + an Argument list looks like a call of a constructor, this is not true!
    a) new MyClass() is not doing the new operan on the result of the MyClass() call.
    b) If it were a call of the MyClass() Constructor, then something like new MyClass.MyClass() would be expected.
    But maybe these points irritate more than that they help.

    So the formst is:
    new Type ( OptArgumentlist ) OptObjectOrCollectionInitializer
    new Type ObjectOrCollectionInitializer

    With kind regards,

    Konrad


    Monday, September 5, 2011 8:23 AM
  • Hi sushma,

    To quote the spec, "An instance constructor is a member that implements the actions required to initialize an instance of a class." That's actually a pretty good description on its own. A constructor is invoked when you use the "new" operator, or use the various methods of reflection to create an instance of a class.

    A constructor looks very much like a method, with a name which is the same as the name of the class, but without a return type.
    You can test with a constructor with your defined class, append this line in the end of the constructor:
    return;
    Is any error thrown?
    Change this line to:
    return this;
    Or return something else, is any error thrown now? If so, you can find some useful info in the error.

    Now let take a look at this:
    new SomeClass();
    Here our target is to explain the new keyword, so we just suppose that we are instantiating a class. We know that a reference to an instance of the SomeClass type is got from the above line, now let's have a look on at what the new keyword does here:
    The following paragraphs are quoted from the book "CLR via C# 3rd Edition":
    ----------------------------
    Here’s what the new operator does:
    1. It calculates the number of bytes required by all instance fields defined in the type and all of its base types up to and including System.Object (which defines no instance fields of its own). Every object on the heap requires some additional members—called the type object pointer and the sync block index—used by the CLR to manage the object. The bytes for these additional members are added to the size of the object.
    2. It allocates memory for the object by allocating the number of bytes required for the specified type from the managed heap; all of these bytes are then set to zero (0).
    3. It initializes the object’s type object pointer and sync block index members.
    4. The type’s instance constructor is called, passing it any arguments (the string "ConstructorParam1" in the preceding example) specified in the call to new. Most compilers automatically emit code in a constructor to call a base class’s constructor. Each constructor is responsible for initializing the instance fields defined by the type whose constructor is being called. Eventually, System.Object’s constructor is called, and this constructor method does nothing but return. You can verify this by using ILDasm.exe to load MSCorLib.dll and examine System.Object’s constructor method.
    After new has performed all of these operations, it returns a reference (or pointer) to the newly created object. In the preceding code example, this reference is saved in the variable e, which is of type Employee.
    ----------------------------

    Have a nice day,


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Leo Liu - MSFT Monday, September 12, 2011 5:25 AM
    Wednesday, September 7, 2011 4:11 AM