Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
constructors are used to initialize fields of class.

That we can do using declaring methods inside a class. like:

class A
{
  public int a,b;

  public void initializeFields(int x, int y)
  {
    a = x; b = y;
  }
}

class MainClass
{
    static void Main()
    {
        A a = new A();
        a.initializeFields(10,20);
        Console.Read();
    }
}

Instead of writing above code, why we go for constructors ?? Like: 

class A
{
  public int a,b;

  public A(int x, int y)
  {
    a = x; b = y;
  }
}

class MainClass
{
    static void Main()
    {
        A a = new A(10,20);
        Console.Read();
    }
}

I want to know the ADVANTAGE , SIGNIFICANCE of using the later code ??


What I have tried:

I want to know the EXACT reason
Posted
Updated 7-Aug-17 0:51am
Comments
F-ES Sitecore 7-Aug-17 6:27am    
I'm sure if you google you'll find lots of articles discussing constructors and their value, but one good reason is that you can't fail to set the values using constructors as this code won't compile

A a = new A();

It lets the developer know the object needs some default values, whereas with the other method what happens if initializeFields is never called? How does the developer know that method needs to be called?
Kingshuk_SP 8-Aug-17 5:38am    
@F-ES Sitecore

i think this one make sense. Perfect.
BillWoodruff 7-Aug-17 7:46am    
They are an essential feature of the C# language. What if asked you the exact reason why your body had blood ?

You are wasting your time repeating this silly question which has already been voted down.
Kingshuk_SP 8-Aug-17 5:30am    
@BillWoodruff

buddy, first understand what you are saying..
Body needs only "BLOOD", there is no alternative.. only one way..

But what I have asked here is, "There are TWO ways.. Already a way we can initialize members of a class (Program 1) , then why we should go for constructors..(Program 2)??"

You don't understand the question properly may be.., that's fine.
But before comment something, just think twice about that if that is justified.
Thank you.

1 solution

There is no "exact" reason - they do a multitude of jobs!

Constructors allow you to specify that an object can either be created in various different ways, or that it must have information given when it is constructed. They also allow us to create an object completely before it is ever used - which can include loading data from a database, or a disk file; connecting to a website to check that the licence to use the class is valid, or pretty much anything else we need.
The first thing is: every object must have a constructor. If you do not define one, then a default parameterless constructor is created by the system for you, which does nothing. If you do define a constructor, then that default is not created, so creating a constructor yourself allows you to say "this class needs this information and cannot be used without it". For example, you might have a Circle class that must have a Radius passed to it when it is built.
This allows us to use class instances later in the full knowledge that they will work: you can't create a Circle that doesn't have a radius, so the Area property will always work for example.
You can also overload the constructor to let your user be more flexible in how to create your class - for a Circle, you might provide a constructor that takes the Radius, and one that takes the Area, and one that takes the Circumference - because you can create your Circle with all the info you need from any one of those.

You can also create a private constructor which means that the outside world cannot create any instances of the class themselves - they have to use methods in your class to do that, which lets you have much tighter control over how many instances they get to play with. This is used in the Singleton pattern to ensure than one and only one instance of a class is ever created.

There is a lot going on here, so google a bit and read some of the articles on them - this is a good one: Constructors in C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900