Member Avatar for Falcon25

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

Recommended Answers

All 4 Replies

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
}
Member Avatar for Falcon25

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

Gonna mark this as solved.

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.