Why does the String class not have a parameterless constructor?

前端 未结 8 606
萌比男神i
萌比男神i 2021-01-11 11:07

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

8条回答
  •  庸人自扰
    2021-01-11 11:40

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

提交回复
热议问题