Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable string join in C# .NetStandard 2.0

I'm trying to write a library with .NetStandard 2.0, and apparently there's not an overload for the string.Join method that accepts an IEnumerable. This code works fine in .NetCore 2.0, but not standard:

string.Join('/', parts.Skip(index))
like image 464
eltiare Avatar asked Dec 14 '22 17:12

eltiare


2 Answers

The overload exists with a string separator instead of just char separator:

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

so... use that?

like image 139
Marc Gravell Avatar answered Jan 05 '23 13:01

Marc Gravell


To add a bit of context to the answer by Marc Gravell: .NET Standard and .NET Core have a different set of APIs.

.NET Standard represents a set of APIs that need to be implemented by a platform if it supports a version of .NET Standard.

.NET Core is a platform that implements .NET Standard. In addition to those APIs, it implements a few more.

The APIs available on .NET Standard for string.Join are (from 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; }

For .NET Core, the set of APIs is bigger since APIs were added to the .NET Core platform but not to .NET Standard (from 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; }

If you are targeting .NET Core, you can use the overload that takes a char. If you are targeting .NET Standard, you can use the overload that takes a string.

like image 33
Alex Ghiondea - MSFT Avatar answered Jan 05 '23 11:01

Alex Ghiondea - MSFT