Вопрос

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

Это было полезно?

Решение 2

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.

Другие советы

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.

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

As said before, strings are immutable and therefore if you manipulate a string you actually create a new one every time.

Example:

string s = "str"; // str was created in the memory.
s += "2"; // str2 was created in the memory.

Use StringBuilder when you want to manipulate string(that's why you wanted an empty ctor, right?)

Why indeed?

It would be completely logical and sensical to provide a parameterless constructor for the string type, yet it doesn't have one.

The reason is because the designers of that type thought it would be a much better idea to have string.Empty.

There could be a logical reason for having the ability to construct multiple empty strings that are different instances. I fail to see one off the top of my head, but that doesn't mean someone else can't see one.

There are some technical reasons behind why limiting the usage to string.Empty might be a good idea. First, all empty strings are considered equal, though not necessarily ReferenceEquals, so having multiple empty strings would seemingly make no sense. The second you say that "I have these two seemingly similar things, yet I've attached a different meaning to each" then perhaps you're trying to solve a problem with the wrong tool.

There's also some upshots of having a predefined string.Empty. Whenever you reference it, you're referencing the same object instance as every other place, and thus you don't have lots of empty (and identical) string objects in memory.

But could it be done? Sure.

So while everybody here has tried to justify that there should be no such constructor, I am saying that there could be such a constructor.

However, someone decided to design the type without one.

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

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.

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 :)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top