Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do String.Copy in .net core?

Tags:

c#

.net-core

uwp

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);
    }
like image 963
fluter Avatar asked Jan 20 '17 07:01

fluter


People also ask

How do I copy a string in C#?

Copy(String) Method is used to create a new instance of String with the same value as a specified String. In other words, this method is used to copy the data of one string into a new string.

How do you copy a string?

strcpy(): Using the inbuilt function strcpy() from string. h header file to copy one string to the other. strcpy() accepts a pointer to the destination array and source array as a parameter and after copying it returns a pointer to the destination string.

What is copy method in C#?

In C#, Copy() is a string method. It is used to create a new instance of String with the same value for a specified String. The Copy() method returns a String object, which is the same as the original string but represents a different object reference.

What does string clone do?

In C#, Clone() is a String method. It is used to clone the string object, which returns another copy of that data. In other words, it returns a reference to this instance of String. The return value will be only another view of the same data.


1 Answers

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);
like image 159
Kyle Stay Avatar answered Oct 07 '22 21:10

Kyle Stay