13

In porting a .net framework app to .net core app, there are some uses of String.Copy to copy strings. But it looks this method is removed from .net core, so how would you copy a string in .net core app, and as a result, it is not present in uwp too. Does assignment string b = a; in .net core means something different as in .netframework?

The copy is used in this code:

public DataDictionary(DataDictionary src)
        :this()
    {
        this.Messages = src.Messages;
        this.FieldsByName = src.FieldsByName;
        this.FieldsByTag = src.FieldsByTag;
        if (null != src.MajorVersion)
            this.MajorVersion = string.Copy(src.MajorVersion);
        if (null != src.MinorVersion)
            this.MinorVersion = string.Copy(src.MinorVersion);
        if (null != src.Version)
            this.Version = string.Copy(src.Version);
    }
fluter
  • 13,238
  • 8
  • 62
  • 100
  • 4
    Why would you use the `Copy` method? – Patrick Hofman Jan 20 '17 at 07:37
  • 3
    No, it means just a reference copy in both cases. Do you know *why* the app is using `String.Copy`? It's pretty unusual to need ti, and is generally *worse* than using `=` (as it involves creating a copy in a way that is almost always unnecessary). – Jon Skeet Jan 20 '17 at 07:38
  • 1
    Related: [What's the use of System.String.Copy in .NET?](http://stackoverflow.com/q/520895/15498) – Damien_The_Unbeliever Jan 20 '17 at 07:39
  • tried `new String(a)`? – Sunny R Gupta Jan 20 '17 at 07:39
  • @JonSkeet It's a user defined class with string members, and `string.Copy` was used in the copy constructor. – fluter Jan 20 '17 at 07:41
  • @SunnyRGupta The constructor `String(string) ` is not present in .net core too. :( – fluter Jan 20 '17 at 07:43
  • 3
    That doesn't explain *why* it's being used. Are you able to ask the original developer? It's possible that they didn't understand that strings are immutable in .NET. If they're really trying to ensure they have separate instances so they can lock on them or something similar, I would try to fix that poor practice... – Jon Skeet Jan 20 '17 at 07:46
  • Possible duplicate of [What's the use of System.String.Copy in .NET?](http://stackoverflow.com/questions/520895/whats-the-use-of-system-string-copy-in-net) – Andrii Krupka Jan 20 '17 at 08:43

3 Answers3

17

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);
Kyle Stay
  • 299
  • 3
  • 6
  • This was the first search result on Google when search for "string.copy c# .net core". Also, my answer does provider a solution the title question of "How to do String.Copy in .net core?". – Kyle Stay Apr 23 '17 at 12:57
  • Question itself not about the direct copying, but about the `Copy` method – VMAtm Apr 23 '17 at 21:26
  • So... I should have asked and then answered my own question? – Kyle Stay Apr 30 '17 at 12:56
  • 2
    Embedded in the "question" is "so how would you copy a string in .net core app". This answers that question. Your downvote is invalid. – EthR May 02 '17 at 17:55
  • 1
    This answers the question, should be the accepted answer. – ejcortes Oct 24 '19 at 20:21
6

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.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Note that, [unlike JavaScript](http://stackoverflow.com/questions/41458001/why-to-avoid-creating-objects-in-javascript), `new String(new char[]{'a'}) == new String(new char[]{'a'})` is still `True`. – devRicher Jan 20 '17 at 07:49
  • Due to interning I guess. That is the 'problem' `string.Copy` solves. – Patrick Hofman Jan 20 '17 at 07:50
  • Bear in mind that string overrides [==](https://msdn.microsoft.com/en-us/library/system.string.op_equality(v=vs.110).aspx) so it's not about interning. – Damien_The_Unbeliever Jan 20 '17 at 07:54
  • Ah, of course. Good point @Damien_The_Unbeliever. (As a side question: why does `string.Copy` not work then?) – Patrick Hofman Jan 20 '17 at 07:55
  • Thanks, in the code some members are assigned, some are copied, maybe I will just assign them all and see if any test breaks. – fluter Jan 20 '17 at 07:55
  • 2
    The correct answer, as answered by Kyle Stay below is `var copiedString = new string(stringToCopy.ToCharArray());`. There are valid instances where you want two different string instances of the same value. For example, unit testing code that may manipulate the memory of the string reference. It's rare, but valid. – Michael Brown Jan 05 '20 at 00:14
2

string newstring = $"{oldstring}";

  • 1
    Please consider explaining your code and how it helps resolve the question. – Studocwho Jan 24 '19 at 18:17
  • Copying any object is an inherently dangerous act. Normally when I want a copy of a string, I want an independent copy; one that, if I somehow change it, I don't change the thing I copied from. String interpolation accomplishes this with the least amount of keystrokes. – Greg Higgins Feb 22 '19 at 18:36
  • I just tested this and the new string was still an object reference match with the old string however `string b = $"{a} ".Trim();` did work, but not with the space removed. – MetaGuru Sep 09 '20 at 13:38