I have two extension methods:
public static string ToString(this List<object> list, char delimiter)
{
return ToString<object>(list, delimiter.ToString());
}
public static string ToString(this List<object> list, string delimiter)
{
return ToString<object>(list, delimiter);
}
When I use this:
char delimiter = ' ';
return tokens.ToString(delimiter);
It won’t work. The char overload doesn’t show up in the code completion list either. Can anybody tell me how to make this work?
EDIT
I accidentally forgot to mention that there are in fact 3 extension methods, the third being:
public static string ToString<T>(this List<T> list, string delimiter)
{
if (list.Count > 0)
{
string s = list[0].ToString();
for (int i = 1; i < list.Count; i++)
s += delimiter + list[i].ToString();
return s;
}
return "";
}
Add reference to the class in which you have the extension methods:
VS / ReSharper doesn’t offer to add reference automatically simply because the method is already recognized, just not with that particular signature.
Also, your methods themselves don’t compile unless you have a third extension methods with generic parameter.
The way they work for me (compile and logically):
Usage will then be:
If you want to avoid casting and make the methods generic, change them to:
And then the usage is much cleaner:
EDIT
So after your edit I understand your problem better.
You should lose the non-generic methods and have generic overload to accept char as well.
Also, the logic you are trying to implement can be easily achieved with:
So you can basically delete all of those methods and just use that, unless you really want it as an extension method for lists.