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

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

问题描述

在将 .net 框架应用程序移植到 .net 核心应用程序时,有一些使用 String.Copy 复制字符串.但看起来此方法已从 .net core 中删除,那么您将如何在 .net core 应用程序中复制字符串,结果,它也不存在于 uwp 中..net core 中的赋值 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天全站免登陆