Ram
Ram

Reputation: 11644

Empty constructor or no constructor

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

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

Is it a best practice to have an empty default constructor?

Class test
{
   public test()
   {

   }
        ......
}

or

Class test
{
        ......
}

Upvotes: 79

Views: 69072

Answers (8)

Soe Moe
Soe Moe

Reputation: 3438

If the class won't be used by third parties and you don't need an overloaded constructor, don't write an empty constructor.

But...

Imagine you already shipped the product, and third parties use your class. A few months later there's a new requirement that makes you add a constructor with an argument.

Now, by doing so, the C# compiler no longer generates a default constructor. If you don't add an empty constructor explicitly, the third party code will be break.

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

Upvotes: 96

Stewart
Stewart

Reputation: 4260

If you don't need a parameterless constructor, don't add one with an empty body. However, if someone wants to serialize/deserialize objects of your class, then you'll need a parameterless constructor.

If you define no constructors, then the compiler will automatically generate a default parameterless one for you, but it won't if you define a constructor yourself.

Incidentally, you can suppress the C# compiler's automatically generated default constructor by defining an empty, private constructor.

Upvotes: 6

this. __curious_geek
this. __curious_geek

Reputation: 43207

There's nothing like default constructors. It's public parameter-less constructor in C#. C# compiler will look for constructors in a class while compiling and will add a public parameter-less constructor if no constructors are defined. So it's ok to skip defining a constructor if your class objects don't need any special operation for construction.

Secondly, if you define one constructor with parameter then compiler will not add a public parameter-less constructor because compiler will assume your objects must be constructed via the constructor you've defined. Having said that, you must explicitly define a parameter-less constructor if you have at least one parameterized constructor, if you need your objects to construct with parameteress constructor.

You can also have a private parameterless constructor as well that is instrumental in making singleton classes.

Upvotes: 12

Nate
Nate

Reputation: 5397

The only times I would include an empty parameterless constructor is if I want the constructor to be private or if I want a parameterless constructor to call another overload with null as the parameter. BTW, in your example above you've made Test's constructor private by not specifying public.

class Foo
{
    public Foo()
        : this(null)
    {
    }

    public Foo(string bar)
    {
        if (bar != null)
        {
            Console.WriteLine(bar);
        }
    }
}

Upvotes: 2

Igor Zevaka
Igor Zevaka

Reputation: 76500

KISS - If your class doesn't need to do anything in the default constructor - don't define it. The compiler will generate one for you.

From the horse's mouth:

Unless the class is static, classes without constructors are given a public default constructor by the C# compiler in order to enable class instantiation.

Upvotes: 38

Nimesh Madhavan
Nimesh Madhavan

Reputation: 6318

By default C# will add a zero parameter constructor for you. So don't add one if you don't have anything special to do there.

You will have to add any empty constructor yourself - even without code - if you have another constructor with parameters and want have to keep the parameter-less constructor alive.

Another scenario where you would want to add an empty parameter-less constructor is if you want to change its access modifier from public to something else(private, protected or internal)

Upvotes: 4

alejandrobog
alejandrobog

Reputation: 2101

Dont write an empty constructor it would be created by default

Upvotes: 0

Alan
Alan

Reputation: 46813

The C# compiler will create the default constructor for you, so you should not write parameterless one, with an empty body.

Upvotes: 2

Related Questions