Can someone, unambiguously and without too much technicality explain to me why would one want to have a Constructor in their program.

I understand what a Constructor is, and that it can be overloaded and it's name is the same as the class and it can't have a return type and that the compiler always secretly makes one by default...That's fine..

But why would I want to have one? Why can't I just use a normal method instead....

Also, does this resemble a Constructor-Like procedure? ->

Human human = new Human();

Please answer both of my questions with as less technicality as possible please. Treat me as a baby.

Thank you!

UsSy

Recommended Answers

All 5 Replies

Your call to create your new Human object is calling the constructor method for the Human class, it is not itself a constructor. Calling the constructor of a class is the only way of creating a new instance of your class. You can have a different method that returns a Human object, but somewhere in that method it must call a constructor to create the instance.

Ahah! Yep, makes sense.

Right so erm, please let me make this even more clear to myself and perhaps any other forum user who doesn't quite understand the use of constructors.

So the only way to make an instance of a class is through the use of a constructor.

If I wanted to create an instance of a certain class within a method, I will have to call the constructor of that class and make a new object there and then.

Now to clarify what use a Constructor is.

Constructor's create instances of a class. So if I have a C# application with a lot of classes and I wish to access the methods or objects from a certain class then I can create an instance of that particular class and use the accessor (.) to access that class. Hence why we need a constructor.

Bingo?

You can use a default constructor to give the fields of your class initial values.
This avoids the use of an extra Initialisation method.
You can also overload constructors, if you want some fields to be initialised with other then the default values.

Can you please show me an example of what you said. For both suggestions please.

UsSy

Here is a sample class:

enum Sex {Unknown, Male, Female};

public class Human {
    Sex sex;
    DateTime birthday;

    public Human() {
        sex = Sex.Unknown;
        birthday = DateTime.Now();
    }

    public Human(Sex s) {
        sex = s;
        birthday = DateTime.Now();
    }

    public Human(DateTime d) {
        sex = Sex.Unknown;
        birthday = d;
    }

    public Human(Sex s, DateTime d) {
        sex = s;
        birthday = d;
    }
}

The first constructor (line 7) is the default constructor. It creates a Human object and sets the internal variables to specific values. The various other constructors (Lines 12, 17, and 22) let you specify what these internal variables are when you create the object.

Yes, this isn't a useful class, just an example :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.