Yes, i'm a noob - Bite me.

And yes, I've read seemingly countless tutorials which explain, or, "try" to explain what a "Constructor" is. Obviously, they failed to digest in my brain.

Is there any well-to-do person in here who can help me? Is there anyone who can possibly explain in this forum or add me on msn/skype and explain over there?

Please try not to answer like this: "A constructor works by initializing the variable of instances by casting an object with polymorphic member variables who are attached to an int64 stringless thing" i.e. - less terminology as possible please =)

Any help is weally weally weally appreciated - weally.....

Kind regards,

Usmaan

Edited 6 Years Ago by Usmaan: n/a

A constructor is a method in a class that is called when that class is first initialized. A constructor allows you to set default values to variables and limit instantiation (among other benefits.

If a constructor is not defined in the class the CLR (Common Language Runtime) will provide an implicit one (known as a Default constructor). A class can have multiple constructors (called constructor overloading) as long as arguments are not of the same type.

Constructors:

  • Can never return a value
  • Can be overloaded
  • Can be static or non-static

I wouldn't define a constructor as a method, because a constructor will NEVER return a value because there's no return type. You're gonna say, what about the void methods. 'void', 'int', 'string' or any kind of object is a return type, the difference between void and the rest is that void will simply call

return;

without any value.

To get back to the constructor, a constructor is what "construct" the class, in the programming slang, we call it 'initialization/instantiation of an object'.

The 'call' to the constructor is done when you initalize an object, which is done with a the 'new' keyword.

i.e.:

DaniWebUser user = new DaniWebUser();

In this trivial example, which you have done probably many times by now, the first DaniWebUser (on the left side of the equal), is the type of the object, which represent the class name in your other class. And then the second DaniWebUser() is the 'call' to the constructor.

Something to note, as Psychocoder pointed out, if you create a new class and do not define a constructor, a default empty one will be provided. However as soon as you define a constructor yourself, the default one won't be provided anymore.

Now ok cool, really interresting, but what's the point of having 'custom' constructor.

Constructor are there to do the mandatory steps or/and fill the mandatory fields (variables or properties) needed for your object to act properly. If we use the previous example, technically it is correct, however, what's the point of having an user if you cannot login anywhere, right? Isn't that why in all the forums, the mandatory fields are always username and password?

DaniWebUser user = new DaniWebUser("PierlucSS", "ilovedaniweb");//dont try this it wornt work ;)

Which means the signature of the constructor will be !?

public class DaniWebUser
{
     public DaniWebUser(string username, string password)
     {
         //then here you could assign the values passed to your class' to your class variable's
     }
}

Hope this is clear and will help you :)

Unfortunately I'd have to disagree, a constructor is a method. Take a look (search for it). On MSDN it says:

Constructors are class methods that are executed when an object of a given type is created. Constructors have the same name as the class, and usually initialize the data members of the new object.

At csharp-online is says:

Method called automatically when an object is created. The constructor initializes the new object to a valid state by setting member variable values. The constructor has the same identifier as its class.

Unfortunately I'd have to disagree, a constructor is a method. Take a look (search for it). On MSDN it says:

At csharp-online is says:

I agree, it's true, I didn't say that it wasn't a method, I said I wouldn't define it as a method since it doesn't behave as a method.

Constructors do return variables. It is implicitly doing "return this;" at the end. Because the expectation is that the constructor always returns the instance it constructed, one doesn't have to explicitly state what type of variable is returned.

And for the OP: It's the function that makes the variable, and describes what should be done when it's made.

Okay - This is making desent sense to me.

Right, i'm going to give shit a shot so, correct me if I am wrong.

A constructor is basically a method but doesn't act exactly as how a method acts.

Namely, a constructor has the same name as the class, can be overloaded, does not have a return type and the CLR (Common Language Runtime) makes a default one if you haven't already made one and can be static or non-static (What does static do?....).


An example of a constructor in action:

Internet newWebsite = new Internet();

Where Internet is a class, newWebsite is an object of type Internet and Internet() is the constructor of the Internet class (Oh boy...).

So right now, the compiler is calling the constructor of the Internet class and making an instance of of called newWebsite - BUT, it's not inizialised as the Internet class has a default constructor. Therefore, we can make our own constructor, with it's own parameters etc.

Coolio.

But...Can someone show me an example of where a method won't be enough and a constructor will be necessary. I understand how a constructor works, but, I don't understand WHY or HOW is it needed in real life...etc...

Thank you.

No where. You can simply use an usual method, instead of cnstructor.
Something like:

//on class1:
Class2 c2 = new Class2();
c2.DoOnBeginning();

//on Class2:
class Class2
{
   pubic Class2()
   {
        //constructor of class2 (its empty - but in use now)
   }

    public void DoOnBeginning()
    { 
       //do some code!
    }
}

Constructor its only a method which is called the 1st one when you instantiate a new class, so when you do:

Class2 c2 = new Class2(/*here goes 1st parameter directly to constructor of class2*/);

You can have even more constructors:

//on class1:
int intValue = 1;
string strValue ="a";

Class2 c2 = new Class2(intValue);  //1.
Class2 _c2 = new Class2(strValue); //2.

//on Class2:
class Class2
{
   pubic Class2(int intValue)      //1. This will be called by c2 instance!
   {
        
   }

   pubic Class2(string strValue)   //2. This will be called by _c2 instance!
   {
        
   }
}

I hope you see the difference in using Constructor(s). It is simply nothing special aobut it, but the advantage of it is, as said, it is the 1st called method when creating a new instance of a class.

Mitja

This question has already been answered. Start a new discussion instead.