Why does the String class not have a parameterless constructor?

前端 未结 8 533
萌比男神i
萌比男神i 2021-01-11 11:07

int and object have a parameterless constructor. Why not string?

相关标签:
8条回答
  • 2021-01-11 11:26

    Update:

    To provide more information for you.

    You don't have an empty Constructor with a string, however you do have String.Empty. The reason is because a string is an immutable object every instance of a string you modify is actually creating a new string in memory.

    For instance: string name = ""; though it is an empty string it will still hold around twenty bytes. Where the string.Empty will only hold around four or eight bytes. So though they mean the same thing, one is more efficient than the other.

    However I believe you want an empty Constructor to do manipulation that may be more commonly handled by the StringBuilder. Some really nice usage between the two can be found here (Determine performance hit / usage).

    Some additional information on the string can be found here. They are immutable thus the contents cannot be changed afterwards.

    Example:

    string first = "Greg "; // Creates string "first" in memory.
    string last = "Arrigotti "; // Creates string "last" in memory.
    string name = first + last; // Creates string "name" in memory.
    

    As you edit one of these, it is simply creating a whole new string in memory. If you are looking at a way to potentially handler user data in a field where no middle name exist for instance, the empty string may contain valid usage.

    Hopefully these point you in the proper direction.

    0 讨论(0)
  • 2021-01-11 11:26

    Also there is already a defined constant for this: String.Empty

    0 讨论(0)
  • 2021-01-11 11:27

    Strings are immutable, therefore new String() has no purpose. What would you do with it?

    0 讨论(0)
  • 2021-01-11 11:27

    int is a value type, and as such it must have a parameterless constructor. There is no consideration that can be made here.

    object has no reason to have anything but a parameterless constructor. There is no data to give it. What parameters would you expect it to take? objects constructed with a parameterless constructor also have a purpose; they are used, for example, as objects to lock on. It is however a class, so it doesn't need to have a public parameterless constructor, however since it has no need for parameters, it's a question of whether you want instance of it to be constructed at all; Microsoft chose to make it concrete, rather than abstract.

    string is a class, so it isn't required to have a parameterless constructor. The team building it simply never saw a need to have one. One could sensibly use such a constructor to create an empty string, but they choose to expose string.Empty (as well as an empty string literal) as a way of explicitly creating an empty string. Those options have improved clarity over a parameterless constructor.

    Another pretty significant advantage of string.Empty and the empty literal string is that they are capable of re-using the same string instance. Since strings are immutable, the only way to observe the difference between two different references to empty strings is through the use of ReferenceEquals (or a lock on the instance). Because there is virtually never a need to go out of your way to have different references to an empty string, removing the parameterless constructor removes the possibility of an equivalent but poorer performing method of constructing an empty string. In the very unlikely event that it is important to construct a new string instance that is an empty string, an empty char array can be passed to the relevant constructor overload, so removing the parameterless constructor doesn't remove any functionality from the end user; it simply forces you to go out of your way to do something really unusual if you want to do something really unusual, which is the sign of good language design.

    0 讨论(0)
  • 2021-01-11 11:30

    Because there is no point in doing that.

    string is immutable. Creating an empty string is just useless.

    MSDN:

    Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this.

    As Jonathan Lonowski pointed out, we have string.Empty for that.

    0 讨论(0)
  • 2021-01-11 11:38

    Provided that you know that string is immuable, your question can be rephrased as the following:

    why on earth can't I initiate a null object??

    answer:

    Because there is no null object :)

    0 讨论(0)
提交回复
热议问题