why is there not always a default constructor [duplicate]

白昼怎懂夜的黑 提交于 2019-12-11 16:45:45

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/16137767/why-is-there-not-always-a-default-constructor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!