none
Constructor return type

    General discussion

  • Hi all,

    I was wondering if anyone can tell me what actually does  a constructor return, is it a pointer or some generic object type.

    public class MyClass

    {

    public MyClass()

    {

    Console.WriteLine("my class");

    }

    public static void main()

    {

    MyClass obj= new MyCLass();

    }

    }

    I know obj is an object of MyClass but what value does it actually have, some address or a pointer to an address?


    Shivi Gupta

    Tuesday, January 29, 2013 9:16 AM

All replies

  • Reference to the instance of the MyClass. I think it's just something that references the instance of class and you should not think about them addresses or pointer I have read from somewhere possibly from Eric Lipperts blog.

    Not sure whether return is correct technical term or it that the reference is set to the instance of MyClass.


    • Edited by MasaSam Tuesday, January 29, 2013 9:25 AM
    Tuesday, January 29, 2013 9:24 AM
  • Yes Sir, But that something must have a name. I wanted to know so that I could use some of the return type value or that something, so that it doesn't go to waste. Because at every instance created fo MyClass , the constructor will be called and it will return something.

    Shivi Gupta

    Tuesday, January 29, 2013 9:35 AM
  • Contructor creates new instance of the class and sets the reference.

    I don't know what you are after, but if you don't want to create so many instance then share the reference instead of creating new one; just be sure that the class instance can be shared.

    MyClass obj = new MyClass();
    
    MyClass obj2 = obj;  // now obj2 references the same instance as obj

    • Edited by MasaSam Tuesday, January 29, 2013 9:42 AM
    Tuesday, January 29, 2013 9:41 AM
  • so in the second statement we don't have to call the constructor, that sounds good to me.

    Shivi Gupta

    Tuesday, January 29, 2013 9:44 AM
  • A constructor doesn't return anything. Internally, its return type is void. When you use the 'new' operator, a new instance is created and the constructor is called on that instance (like any instance method). The 'new' operator then returns the instance.
    Tuesday, January 29, 2013 12:11 PM
  • Hi,

    • A class can have any number of constructors.
    • A constructor doesn’t have any return type even void.

    Check the below links in detail about Constructor

    Constructors (C# Programming Guide)

    Constructor types with example programs in C#.NET

    An Intro to Constructors in C#


    PS.Shakeer Hussain

    Tuesday, January 29, 2013 12:34 PM
  • A class can have any number of constructors.

    I didn't say otherwise.

    A constructor doesn’t have any return type even void.

    You cannot declare a return type. It doesn't mean there is none. It's something you find a lot in C#: with very few exceptions, when something can have only one value, you cannot declare it. For example, interfaces members are public: you cannot declare their access modifiers, even 'public'.

    Note also that I said "internally, its return type is void". Look at a compiled class: any method which doesn't return anything has a return type defined as void, including constructors.

    Tuesday, January 29, 2013 1:05 PM
  • That's true, it really is the new operator that does the allocation, invoking the constructor and return or sets the reference, not the constructor itself.
    Tuesday, January 29, 2013 1:17 PM
  • Alright, so obj in the line

    MyClass obj = new MyClass();

    will have the address to some memory location and the constructor when called doesn't return anything, not even an address to a memory location or a pointer?


    Shivi Gupta

    Wednesday, January 30, 2013 8:56 AM
  • As noted by others technically it's not the constructor that sets the reference to the instance it is the new operator and the constructor itselft returns just void.

    In your code the reference of the obj is set to instance of MyClass. So new operator sets or returns, what ever you want to call it, reference to the instance of created class and does much more. If you are interested about the technical details you can check C# specification section 7.6.10 The new operator.

    Wednesday, January 30, 2013 9:11 AM
  • new:

    creates a new Instance. Cares for it to get the memory it needs.
    set's all fields whose starting values were asigned outside of constructor
    calls the constructor on the new isntance whose signature matches the parameters you gave. Wich may call the constructors of the base class or other functions.
    Assigns the values given via Object Initialisers.

    And finally returns you a Referece to the created object that you store in a Reference Varriable.

    You aren't required to save it at all. You could write (parathesis not needed):

    (new Form1()).show();
    And it compiles and works perfectly. You never have to store or process a return value. However in most cases it makes sense to do so. Especially if you want/have to do more more then just one action with the object.

    Wednesday, January 30, 2013 9:45 AM