Ken
Ken

Reputation: 1404

Strange compiling error on C# string.Join

In a uwp function, I am calling this:

var selectedDates = sender.SelectedDates.Select(p => p.Date.Month.ToString() + "/" + p.Date.Day.ToString()).ToArray();
var values = string.Join(", " + (string[])selectedDates);
CalendarViewResultTextBlock.Text = values;

But I got an error when compiling them:

Error CS0121 The call is ambiguous between the following methods or properties: 'string.Join(string, params object[])' and 'string.Join(string, params string[])'

Who knows how to fix it? Thanks.

Upvotes: 2

Views: 1178

Answers (3)

Ted Brownlow
Ted Brownlow

Reputation: 1117

Explanation for the error

This expression turns into a string

", " +(string[])selectedDates

string.Join expects a string followed by a array of parameters. By only providing a string, the array of parameters is inferred to be an empty array. This is not the behaviour you're looking for, but also, the compiler has no way of inferring the type of an empty array

Upvotes: 0

Lukasz Szczygielek
Lukasz Szczygielek

Reputation: 3110

You have a wrong call. It should be

string.Join(", ", array) 

In your example it's + but should be **, **.

Upvotes: 2

mayaht
mayaht

Reputation: 111

Try the following:

var values = string.Join(", ", (string[]) selectedDates );

(Remove the + sign)

Upvotes: 7

Related Questions