1

In C# when I create an empty class it provides a default constructor however when I provide a constructor with parameters the default constructor is no longer created.

My questions are:

  1. why does the compiler no longer give me the default constructor as well?
  2. Is there a setting so that this default constructor is always generated?

These questions arose from working with WCF where I require the default constructor but also want to be able to provide a constructor with values and it would be nice not to have to place the default constructor in every time and I wouldn't think unused default constructors would make much overhead.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Andrew Poland
  • 185
  • 4
  • 14
  • One clarification: Its WCF's default IInstanceProvider that requires a default service constructor... its not a limitation of the framework itself. You can use a custom IInstanceProvider to get around this limitation. – ErnieL Apr 22 '13 at 02:35

5 Answers5

2

Having a custom constructor (usually) means that the internal state of the object is initialized with some custom information that you provide via constructor parameters. If you still had the default constructor in such a case, what would the initial state be?

If you have no custom constructor then it is assumed to be fine if you just use the default constructor because there is no internal state to initialize.

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
0

There is not a setting for it.

In certain cases, the default constructor is a way to enforce a contract that the data be specified.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

A default constructor exists by, well... default. But, if you create your own parameterized constructor, then the compiler assumes that you want to use that one, and does not emit a default constructor anymore.

If you want to have a default to use with WCF, you'll have to specify it. In it, you can call your parameterized constructor with some default values if you prefer.

Gjeltema
  • 4,122
  • 2
  • 24
  • 31
0

In C++, C♯, Java, and their descendant languages, one writes a custom constructor because one needs to ensure that the object is initialized and is in a consistent state. The compiler has no way to guess how it should initialize the object, and so it does not even try. There are many ways to have both a default constructor and a custom constructor for a class.

Java has constructors that refer to each other:

public Name(String givenName, String middleName, String surName) {...}
public Name(String givenName, String surName) {
    this(givenName, null, surname);
}

C++11 has introduced delegating constructors, which do the same thing. C++ has always had default arguments, which can do the same thing too. C♯ seems to have but default arguments.

Other languages use convention: Objective-C has programmers write all initializers in terms of a designated initializer.

In short, there's always a way to do it, but you have to program it by hand. No automatic scheme exists.

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
0

The compiler will only create the default constructor if you did not provide any constructor definition for your class. The reason might be: when you need to create an instance of the object, you will need to call the constructor, so if no constructor defined by the coder, the compiler will add it to make it works.

But once you already define one constructor, that tells the compiler that the user realize the need of the constructor, thus the default constructor will not be automatically add. You need to insert your own default constructor manually. I don't think there is a setting for this.

Ruly
  • 360
  • 1
  • 8