IEnumerable 字符串加入 C# .NetStandard 2.0

IEnumerable string join in C# .NetStandard 2.0

我正在尝试使用 .NetStandard 2.0 编写一个库,显然没有接受 IEnumerable 的 string.Join 方法的重载。此代码在 .NetCore 2.0 中运行良好,但不标准:

string.Join('/', parts.Skip(index))

重载存在 string separator 而不是 char separator:

string s = string.Join("/", parts.Skip(index));

那么……用那个?

为 Marc Gravell 的回答添加一些上下文:.NET Standard 和 .NET Core 有一组不同的 API。

.NET Standard 代表一组 API,如果平台支持某个版本的 .NET Standard,则需要由该平台实现这些 API。

.NET Core 是一个实现.NET Standard 的平台。除了这些 API 之外,它还实现了更多。

string.Join.NET Standard 上可用的 API 是(来自 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; }

对于 .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; }

如果您的目标是 .NET Core,则可以使用带有 char 的重载。 如果您的目标是 .NET Standard,则可以使用带有 string.

的重载