Hi i'm new to C# and programming in general. First of all this site has been very helpful and I was wondering if someone could explain what constructors are to me as simple as possible.

I'm teaching myself at home and the book is either not very good or i'm just preety slow either way I enjoy programming so I'm trying understand it as much as I can.

So, can anyone tell me what constructors actually are and do we need them? The book goes on about overloading constructors but it hasn't actually said what overloading actually is :icon_rolleyes: thank you

Simple:
Constructor is only a method, which has this previlege that is called first when a new instance of a class (or form) is created. It is called automatically when this instance gets created.

The book goes on about overloading constructors but it hasn't actually said what overloading actually is

In simple words Overloading is....

Multiple methods with the same name and different number of arguments or same number of argument list with different data types.

Overloading the constructors is known as Constructor Overloading.

Examples for Constructor overloading:

In a class MyClass

public MyClass()
{
      //Body of the constructor
}
public MyClass(int FirstArg)
{
      //Body of the constructor
}
public MyClass(string FirstArg)
{
      //Body of the constructor
}
public MyClass(int FirstArg,int SecArg)
{
      //Body of the constructor
}

Edited 5 Years Ago by Knvn: n/a

Ok, thanks guys I get the general use of it now.

Gonna mark this as solved.

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