0

After migrating form .net core 2.2 to 3.1 I am getting warning 'string.Copy(string)' is obsolete: 'This API should not be used to create mutable strings.

According MSDN link I didn't find an example of replacing. I found Span but how to implement it no idea.

My field which I am using string.Copy()

var currentVerison = projectForCreationDto.KubesprayCurrentVersion.Trim();
var targetVersion = string.Copy(currentVerison);
Arzu Suleymanov
  • 671
  • 2
  • 11
  • 33
  • Why are you calling `Copy` at all? Are you expecting something to mutate the existing string? – Jon Skeet Apr 27 '20 at 16:24
  • @JonSkeet our requirement is when I post request I would like copy currentVersion data to targetVersion – Arzu Suleymanov Apr 27 '20 at 16:26
  • 5
    Doing a direct assignment will work just fine. `var targetVersion = currentVersion;` – Rufus L Apr 27 '20 at 16:27
  • @ArzuSuleymanov: `currentVersion` and `targetVersion` are just variables. A simple assignment will copy the reference from `currentVersion` to `targetVersion`. In *concrete terms*, what benefit do you expect to gain from making a completely independent string? – Jon Skeet Apr 27 '20 at 16:29
  • 1
    @JonSkeet never thought about it to be honest. As Rufus suggested direct assignment worked fine – Arzu Suleymanov Apr 27 '20 at 16:35
  • @JonSkeet I just wanted to specify only currentVersion and copy value to another string – Arzu Suleymanov Apr 27 '20 at 16:35
  • Yes you can, see the document [system.string.copy](https://learn.microsoft.com/en-us/dotnet/api/system.string.copy?view=netcore-3.1) please. `If you want to create a mutable copy of the string so that you can use unsafe code to modify the string contents, use Marshal.StringToHGlobalUni method. ` – nwpie Apr 28 '20 at 02:07
  • @ArzuSuleymanov,I agree with their suggestion, and you can refer to this:https://stackoverflow.com/questions/520895/whats-the-use-of-system-string-copy-in-net – LouraQ Apr 28 '20 at 05:07

0 Answers0