elt*_*are 2 c# .net-standard .net-standard-2.0
我正在尝试用.NetStandard 2.0编写一个库,显然string.Join接收IEnumerable 的方法没有重载.此代码在.NetCore 2.0中运行良好,但不是标准的:
string.Join('/', parts.Skip(index))
Run Code Online (Sandbox Code Playgroud)
过载存在于string separator而不仅仅是char separator:
string s = string.Join("/", parts.Skip(index));
Run Code Online (Sandbox Code Playgroud)
所以...用那个?
为 Marc Gravell 的答案添加一些上下文:.NET Standard 和 .NET Core 有一组不同的 API。
如果平台支持 .NET Standard 的某个版本,则 .NET Standard 表示需要由平台实现的一组 API。
.NET Core 是一个实现 .NET Standard 的平台。除了这些 API 之外,它还实现了更多 API。
.NET Standard 上可用的 APIstring.Join是(来自https://github.com/dotnet/standard/blob/master/netstandard/ref/mscorlib.cs):
public static System.String Join(System.String separator, System.Collections.Generic.IEnumerable<string> values) { throw null; }
public static System.String Join(System.String separator, params object[] values) { throw null; }
public static System.String Join(System.String separator, params string[] value) { throw null; }
public static System.String Join(System.String separator, string[] value, int startIndex, int count) { throw null; }
public static System.String Join<T>(System.String separator, System.Collections.Generic.IEnumerable<T> values) { throw null; }
Run Code Online (Sandbox Code Playgroud)
对于 .NET Core,API 集更大,因为 API 已添加到 .NET Core 平台但未添加到 .NET Standard(来自https://github.com/dotnet/corefx/blob/master/src/System.Runtime /ref/System.Runtime.cs#L2307):
public static System.String Join(char separator, params object[] values) { throw null; }
public static System.String Join(char separator, params string[] value) { throw null; }
public static System.String Join(char separator, string[] value, int startIndex, int count) { throw null; }
public static System.String Join(System.String separator, System.Collections.Generic.IEnumerable<string> values) { throw null; }
public static System.String Join(System.String separator, params object[] values) { throw null; }
public static System.String Join(System.String separator, params string[] value) { throw null; }
public static System.String Join(System.String separator, string[] value, int startIndex, int count) { throw null; }
public static System.String Join<T>(char separator, System.Collections.Generic.IEnumerable<T> values) { throw null; }
public static System.String Join<T>(System.String separator, System.Collections.Generic.IEnumerable<T> values) { throw null; }
Run Code Online (Sandbox Code Playgroud)
如果您的目标是 .NET Core,则可以使用带有char. 如果您的目标是 .NET Standard,则可以使用采用string.