我试图让以下代码在ubuntu linux上运行的dotnet核心中运行 - 但是得到一个"字符串不包含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;
});
答案 0 :(得分: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上看到它正在运行。
答案 1 :(得分:0)
要完成罗杰森的答案,您可以有一个扩展方法,该方法可以完全满足您的需求。
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));
}
}
}