【问题标题】:String.Copy alternative for dotnet coredotnet 核心的 String.Copy 替代方案
【发布时间】:2016-11-11 14:26:06
【问题描述】:

我正在尝试让以下代码在 ubuntu linux 上运行的 dotnet 核心中工作 - 但在此行上出现“字符串不包含复制定义”编译错误 - 显然 String.Copy 不支持dotnet-core:

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

【问题讨论】:

  • 类似的问题是的,但是这个问题是在另一个问题之前 2 个月被问到的?不知道如何处理。
  • 是的,看起来这个问题得到的关注较少,我的错:(

标签: .net-core


【解决方案1】:

要完成 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));
        }
    }
}

【讨论】:

    【解决方案2】:

    试试这个:

    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上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多