I'm afraid that this is a very silly question, but I must be missing something.
Why might one want to use String.Copy(string)?
The documentation says the method
Creates a new instance of String with the same value as a specified String.
Since strings are immutable in .NET, I'm not sure what's the benefit of using this method, as I'd think that
string copy = String.Copy(otherString);
would for all practical purposes seem to yield the same result as
string copy = otherString;
That is, except for whatever internal bookkeeping that's going on, and the fact that copy is not ReferenceEquals
to otherString, there are no observable differences - String being an immutable class whose equality is based on value, not identity.
(Thanks to @Andrew Hare for pointing out that my original phrasing was not precise enough to indicate that I realized there was a difference between Copy
ing and not, but was concerned about the perceived lack of useful difference.)
Of course when passed a null
argument, Copy throws an ArgumentNullException
, and the "new instance" might consume more memory. The latter hardly seems like a benefit, and I'm not sure that the null check is a big enough bonus to warrant a whole Copy method.
Thanks.