Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
why string class have not constructor take 0 arguments ??
why i can not write this
string text = new string();
why most be give arguments
Posted
Comments
Richard C Bishop 30-Nov-12 16:44pm    
You can't write that because it does not compile, strings are immutable.
Anderso0on 30-Nov-12 16:46pm    
please explain this more
DinoRondelly 30-Nov-12 16:53pm    
string txt = string.empty;

If you want a null string you can simply declare the variable.
C#
string txt;

If you want the string to contain data when you construct it you must tell the constructor what you want. Therefore you must supply parameters to the constructor.

You can, of course also assign a string constant to initialize a string.
C#
string txt = "Hello World";



What is it that you are trying to do that you don't want to supply parameters to the constructor?
 
Share this answer
 
v2
Comments
Anderso0on 30-Nov-12 16:54pm    
i want know why the string class have not constructor defult take 0 arguments ??
Sergey Alexandrovich Kryukov 30-Nov-12 18:41pm    
Please see my answer...
--SA
Anderso0on 30-Nov-12 16:55pm    
because inital string with defult value
Sergey Alexandrovich Kryukov 30-Nov-12 18:41pm    
And?
Sergey Alexandrovich Kryukov 30-Nov-12 18:41pm    
Right, a 5.
(And I tried to explain why it is decided this way...)
--SA
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
 
Share this answer
 
v2
Comments
BillW33 1-Dec-12 6:50am    
Much better than my explanation; +5
Sergey Alexandrovich Kryukov 1-Dec-12 19:54pm    
Thank you very much.

(I explained that there was an accident with my vote; I did not even know it was my vote; I checked it up I found it was mine, by accident... -- sorry. Fixed now. By the way, thank you for bringing my attention to this matter. If you have doubt like that, better don't hesitate to mention it -- accidents happen. :-)

Cheers,
--SA
There is no parameterless constructor because, in .NET, once created, a String cannot be changed. So if the String cannot be changed, what's the point of a parameterless constructor? The only thing you can do with such a constructor is create an empty String object (0 length), which is identical to saying:
string myString = String.Empty;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Dec-12 20:05pm    
Exactly. A 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900