ebousha asked:
I want to know why the class "string" has no default constructor which takes 0 arguments? [grammar improved :-) — SA]
Out of curiosity, this is a reasonable and even interesting question, but not the one which can require a
rational answer. The lack of such constructor, as well as its presence, does no violate laws of nature, .NET design or good software practices. The only really correct answer would be: "because the creators of the FCL decided to do so". They could easily make this constructor available and equivalent to assigning null of
string.Empty
. And pretty obvious reason of such decision is this:
there is absolutely no practical need in such constructor. Anyone can assign null or
string.Empty
; and, as the explicit assignment,
it won't leave any doubt.
(Just one remark: null vs empty strings has a long history of rather lame arguments between programmers, and I see a considerable value in using both; and making explicit difference is a way of alleviation of those lame controversies. :-))
Some background: one thing you should know: if you define no constructors, a parameterless constructor is implicitly defined, for developer's convenience. If you add any constructor with any parameters, it "undefined" implicit parameterless constructor. This is the case with
System.String
. To make a parameterless constructor defined again, you would need to do it explicitly.
[EDIT]
Of course, Dave (see Solution 3) is right. If string was mutable, this constructor would make some sense. With non-mutable strings, you construct the string over and over...
—SA