Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the String class not have a parameterless constructor?

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

like image 631
burnt1ce Avatar asked May 01 '14 14:05

burnt1ce


People also ask

Can constructors be Parameterless?

Parameter-less ConstructorWhen a constructor is declared without any parameter or argument, then it is called a parameter-less constructor. A parameter-less constructor works like a default constructor and this constructor can contain statements, or it can be empty.

What is a Parameterless constructor?

A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new . For more information, see Instance Constructors.

Why can struct have Parameterless constructor?

Although the CLR allows it, C# does not allow structs to have a default parameter less constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor.

Why are string constructor not used very often?

There is rarely a point to using the String constructor (pretty much only when you've created a substring of a very large string and want to release the memory used by the rest of the string, because substrings by default use the same underlying char array as the original string, just with a different offset and length ...


1 Answers

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.

like image 75
Patrick Hofman Avatar answered Sep 28 '22 03:09

Patrick Hofman