How should I initialize an empty string in C# and why? Which one is faster?
string myString = "";
or
string myString = "".ToString();
String.ToString() just returns the string itself :
public override String ToString() {
Contract.Ensures(Contract.Result<String>() != null);
Contract.EndContractBlock();
return this;
}
Which means string myString = "".ToString()
is pointless. After all, it's already a string
In fact, given that strings are immutable and, in this case, interned, ""
is the same string instance returned by String.Empty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With