String.Join overload not recognized
Asked Answered
R

2

6

I have code equivalent to String.Join(',', new List<ulong>()) in a .NET Standard 2.0 project. I get two error from this line:

  1. Argument 1: cannot convert from 'char' to 'string'
  2. Argument 2: cannot convert from 'System.Collections.Generic.List<ulong>' to 'System.Collections.Generic.IEnumerable<string>'

These are the overloads of String.Join ReSharper shows on navigating to symbol:

enter image description here

I would assume that the second-last overload public static string Join<T>(char separator, IEnumerable<T> values); would be selected, but this doesn't happen.

When I changed my code to String.Join<ulong>(',', new List<ulong>()) (explicitly specifying the generic type), the second error disappeared. Am I doing something incorrectly or is this a bug in VS?

Radioman answered 4/8, 2018 at 20:6 Comment(12)
Why don't you just use "," ?Petrapetracca
I could, but that's just circumventing the problem, isn't it?Radioman
No, and you're misunderstanding how all of the above APIs are meant to be used.Petrapetracca
What type are you trying to assign the output to?Gilkey
@Gilkey Just a stringRadioman
@ClausJørgensen How do you mean?Radioman
@HenkHolterman Yes, but the second-last overload indeed takes an IEnumerable<>? Regaring the downvotes - perhaps not, but I can't improve a post unless I know what's wrong with it.Radioman
netstandard 2.0 doesn't have overload with first parameter of type char. All string.Join overloads have first parameter of type string? Seems like Resharper shows you implementation of netstandard 2.0 not a contract.Waken
I see only 5 String.Join overloads and none of them has char as a first argument though, not sure where did you get these 2 additional overloads.Kosel
Does IntelliSense suggest you overload with first parameter of type char?Waken
@Waken No it doesn't. I was unaware it was even capable of that.Radioman
This is a Resharper's "bug", In VS 2017 without Resharper Go To Definition will display correct overloads.Waken
T
9

.NET Standard does not have String.Join overloads with char as a first parameter, only .NET Core has them.

You have to use String.Join(",", new List<ulong>()).

https://learn.microsoft.com/en-us/dotnet/api/system.string?view=netstandard-2.0

Both R# and Rider show wrong info when navigating to symbol from .NET Core code, I can confirm this.

Tager answered 4/8, 2018 at 20:20 Comment(0)
W
1

.NET Standard 2.0 doesn't have overload with first parameter of type char. All string.Join overloads have first parameter of type string?

Seems like Resharper displays you implementation of netstandard 2.0 not a contract.

standard.System.cs:3279

    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; }
Waken answered 4/8, 2018 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.