Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the use of System.String.Copy in .NET?
    primarykey
    data
    text
    <p>I'm afraid that this is a very silly question, but I must be missing something.</p> <p>Why might one want to use <a href="https://docs.microsoft.com/en-us/dotnet/api/system.string.copy" rel="nofollow noreferrer">String.Copy(string)</a>?</p> <p>The documentation says the method</p> <blockquote> <p>Creates a new instance of String with the same value as a specified String.</p> </blockquote> <p>Since strings are immutable in .NET, I'm not sure what's the benefit of using this method, as I'd think that </p> <pre><code> string copy = String.Copy(otherString); </code></pre> <p>would for all practical purposes seem to yield the same result as</p> <pre><code> string copy = otherString; </code></pre> <p>That is, except for whatever internal bookkeeping that's going on, and the fact that copy is not <code>ReferenceEquals</code> 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 <code>Copy</code>ing and not, but was concerned about the perceived lack of <em>useful</em> difference.)</p> <p>Of course when passed a <code>null</code> argument, Copy throws an <code>ArgumentNullException</code>, 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.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload