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 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?
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
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.
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
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.
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.
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.
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.
- Edited by Christopher84Microsoft community contributor Wednesday, January 30, 2013 9:47 AM