How to do String.Copy in .net core?

string newstring = $"{oldstring}";


Assignment of a string is something else than creating a copy. a = b just sets the reference of both variables to the same memory segment. string.Copy actually copies the string and thus the references are not the same any more.

I doubt however if you need string.Copy. Why would you want to have another reference? I can't think of any common cases you ever want this (unless you are using unmanaged code). Since strings are immutable, you can't just change the contents of the string, so copying is useless in that case.


Given your update with the code that uses string.Copy, I would say it is not useful to use string.Copy. Simple assignments will do if you use DataDictionary in managed code only.


This is not as elegant as string.Copy(), but if you do not want reference equality for whatever reason, consider using:

string copiedString = new string(stringToCopy);

Tags:

C#

.Net Core

Uwp