如何在.net核心中执行String.Copy? [英] How to do String.Copy in .net core?

查看:98
本文介绍了如何在.net核心中执行String.Copy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将.net Framework应用程序移植到.net核心应用程序时, String.Copy 复制字符串。但看起来此方法已从 .net核心,那么您将如何在.net核心应用程序中复制字符串,结果,它也不会出现在uwp中。 .net核心中的赋值 string b = a; 意味着与.netframework中的赋值有所不同吗?

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?

在此代码中使用:

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);
    }


推荐答案

分配字符串除了创建副本。 a = b 只是将两个变量的引用设置为相同的内存段。 string.Copy 实际上是复制字符串,因此引用不再相同。

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.

我怀疑是否您需要 string.Copy 。您为什么还要再参考?我想不出您想要的任何常见情况(除非您使用的是非托管代码)。由于字符串是不可变的,因此不能只更改字符串的内容,因此在这种情况下复制是无用的。

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.

考虑到使用 string.Copy 的代码进行更新,我想说使用 string.Copy 。如果仅在托管代码中使用 DataDictionary ,则简单分配即可。

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.

这篇关于如何在.net核心中执行String.Copy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆