String.Copy dotnet 核心的替代方案

String.Copy alternative for dotnet core

我正在尝试让以下代码在 ubuntu linux 上的 dotnet 核心 运行 中工作 - 但在这一行出现 "string does not contain a definition for copy" 编译错误 - dotnet-core 显然不支持 String.Copy:

         Attendance = String.Copy(markers) };

在 dotnet Core 中执行浅字符串复制的最佳方法是什么?我应该使用 string.CopyTo 吗?

谢谢

//I want to add an initial marker to each record
//based on the number of dates specified
//I want the lowest overhead when creating a string for each record

string markers = string.Join("", dates.Select(p => 'U').ToArray());
return logs.Aggregate( new List<MonthlyAttendanceReportRow>(), (rows, log) => {
     var match = rows.FirstOrDefault(p => p.EmployeeNo == log.EmployeeNo);
     if (match == null) {
         match = new MonthlyAttendanceReportRow() {
             EmployeeNo = log.EmployeeNo,
             Name = log.FirstName + " " + log.LastName,
             Attendance = String.Copy(markers) };
         rows.Add(match);
     } else {
     }
     return rows;
 });

试试这个:

string b = "bbb";
var a = new String(b.ToArray());
Console.WriteLine("Values are equal: {0}\n\nReferences are equal: {1}.", Object.Equals(a,b), Object.ReferenceEquals(a,b));

您可以在 this fiddle 上看到它 运行。

要完成 Rogerson 的回答,您可以使用扩展方法来完成您正在寻找的事情。

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CSharp_Shell
{
public static class ext{
    public static string Copy(this string val){
        return new String(val.ToArray());
    }
}

    public static class Program 
    {
        public static void Main() 
        {
            string b = "bbb";
            var a = b.Copy();
            Console.WriteLine("Values are equal: {0}\n\nReferences are equal: {1}.", Object.Equals(a,b), Object.ReferenceEquals(a,b));
        }
    }
}