Empty constructor or constructor

I think it is not necessary to have a default constructor in the class (C #).

So, in this situation, should I have an empty constructor in the class, or can I skip it?

Is it better to have an empty default constructor?

Class test { public test() { } ...... } 

or

 Class test { ...... } 
+51
c #
Jun 03 '10 at 6:30
source share
8 answers

If the class will not be used by third parties and you do not need an overloaded constructor, do not write an empty constructor.

But...

Imagine that you have already sent the product, and third parties use your class. A few months later, a new requirement appeared that forces you to add a constructor with an argument.

Now, with this, the C # compiler no longer generates a default constructor. If you do not explicitly add an empty constructor, the third code will be broken.

In my opinion, you should always define empty constructors (one liner) for public classes used by third parties.

+55
Jun 03 '10 at 7:45
source share

KISS - If your class does not need to do anything in the default constructor, do not define it. The compiler will generate one for you.

From the horse mouth :

If the class is not static, classes without constructors are given an open default constructor by the C # compiler to include the instantiation class.

+31
Jun 03 '10 at 6:33
source share

There nothing like default constructors . This is an open constructor without parameters in C #. The C # compiler will look for constructors in the class at compile time and will add an open constructor without parameters if no constructors are defined. So it’s normal to skip the definition of the constructor if the class does not require a special operation to build.

Secondly, if you define one constructor with a parameter, the compiler will not add an open constructor without parameters, because the compiler will assume that your objects must be constructed using the constructor you define. Having said that, you must explicitly define a constructor without parameters, if you have at least one parameterized constructor, if you need your objects to build with the parameteress constructor.

You can also have your own constructor without parameters, which plays an important role in creating singleton classes.

+8
Jun 03 '10 at 6:37
source share

If you do not need a constructor without parameters, do not add it with an empty body. However, if someone wants to serialize / deserialize the objects of your class, you will need a constructor without parameters.

If you do not define constructors, then the compiler automatically generates a parameter without parameters for you, but it will not be if you define the constructor yourself.

By the way, you can suppress the automatically generated default constructor of C # by setting an empty, closed constructor.

+5
Jun 03 '10 at 6:41
source share

By default, C # will add a null parameter constructor for you. Therefore, do not add it if you have nothing special.

You will need to add any empty constructor yourself - even without code - if you have another constructor with parameters and you want to save the constructor without parameters.

Another scenario in which you want to add an empty constructor without parameters is if you want to change its access modifier from the public to something else (personal, protected or internal)

+2
Jun 03 '10 at 6:33
source share

The C # compiler will create a default constructor for you, so you should not write without parameters, with an empty body.

+2
Jun 03 '10 at 6:34
source share

The only points when I will include an empty constructor without parameters is that I want the constructor to be closed, or if I want the constructor without parameters to cause another overload with a null parameter. BTW, in the example above, you created the private Test constructor without specifying the public one.

 class Foo { public Foo() : this(null) { } public Foo(string bar) { if (bar != null) { Console.WriteLine(bar); } } } 
+2
Jun 03 '10 at 6:41
source share

Do not write an empty constructor, which will be created by default

+1
Jun 03 '10 at 6:35
source share



All Articles