[英]Why does the String class not have a parameterless constructor?
int
and object
have a parameterless constructor. int
和object
有一个无参数构造函数。 Why not string
? 为什么不
string
?
Because there is no point in doing that. 因为没有必要这样做。
string
is immutable. string
是不可变的。 Creating an empty string
is just useless. 创建一个空
string
是没用的。
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. 正如Jonathan Lonowski指出的那样,我们有
string.Empty
。
Strings are immutable, therefore new String()
has no purpose. 字符串是不可变的,因此
new String()
没有任何意义。 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?) 当你想操纵字符串时使用StringBuilder (这就是为什么你想要一个空的ctor,对吧?)
Update: 更新:
To provide more information for you. 为您提供更多信息。
You don't have an empty Constructor with a string
, however you do have String.Empty
. 你没有带
string
的空构造函数,但是你有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. 原因是因为
string
是不可变对象 ,您修改的string
每个实例实际上都在内存中创建一个新string
。
For instance: string name = "";
例如:
string name = "";
though it is an empty string
it will still hold around twenty bytes . 虽然它是一个空
string
但它仍然可以容纳大约20个字节 。 Where the string.Empty
will only hold around four or eight bytes . 其中
string.Empty
只能容纳四到八个字节 。 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
. 但是我相信你想要一个空的构造函数来进行可能更常被
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 . 有关
string
一些其他信息可以在这里找到。 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. 在编辑其中一个时,它只是在内存中创建一个全新的
string
。 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. 希望这些指向正确的方向。
Why indeed? 为什么呢?
It would be completely logical and sensical to provide a parameterless constructor for the string
type, yet it doesn't have one. 为
string
类型提供无参数构造函数是完全合乎逻辑和理性的,但它没有。
The reason is because the designers of that type thought it would be a much better idea to have string.Empty
. 原因是因为那种类型的设计师认为拥有
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. 将使用限制为
string.Empty
原因有一些技术原因string.Empty
可能是个好主意。 First, all empty strings are considered equal, though not necessarily ReferenceEquals
, so having multiple empty strings would seemingly make no sense. 首先,所有空字符串都被认为是相等的,但不一定是
ReferenceEquals
,所以有多个空字符串似乎没有意义。 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
. 还有一些关于拥有预定义
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. 然而,有人决定设计没有一个类型。
此外,还有一个已定义的常量: String.Empty
int
is a value type, and as such it must have a parameterless constructor. int
是一个值类型,因此它必须具有无参数构造函数。 There is no consideration that can be made here. 这里没有考虑到。
object
has no reason to have anything but a parameterless constructor. object
没有理由有什么,但参数的构造函数。 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. 例如,它们被用作
lock
对象。 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. string
是一个类,因此不需要具有无参数构造函数。 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. 可以合理地使用这样的构造函数来创建一个空字符串,但是他们选择公开
string.Empty
(以及一个空字符串文字)作为显式创建空字符串的方法。 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. string.Empty
和空文字字符串的另一个显着优点是它们能够重用相同的字符串实例。 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). 由于字符串是不可变的,因此观察两个不同的空字符串引用之间差异的唯一方法是使用
ReferenceEquals
(或实例上的lock
)。 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;
在极不可能的情况下,构造一个空字符串的新字符串实例很重要,可以将空的char数组传递给相关的构造函数重载,因此删除无参数构造函数不会从最终用户中删除任何功能。 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.