C# Extension Method Overload

c#, extension-methods, overloading

Solution

Add reference to the class in which you have the extension methods:

using MyApplicationNamespace.ToStringExtensionClass;

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):

public static string ToString(this List<object> list, char delimiter)
{
    return ToString(list, delimiter.ToString());
}

public static string ToString(this List<object> list, string delimiter)
{
    return string.Join(delimiter, list);
}

Usage will then be:

var list = new List<int> { 1, 2, 3, 4, 5 };
var str = list.Cast<object>().ToList().ToString(' ');

If you want to avoid casting and make the methods generic, change them to:

public static string ToString<T>(this List<T> list, char delimiter)
{
    return ToString(list, delimiter.ToString());
}

public static string ToString<T>(this List<T> list, string delimiter)
{
    return string.Join(delimiter, list);
}

And then the usage is much cleaner:

var list = new List<int> { 1, 2, 3, 4, 5 };
var str = list.ToString(' ');

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.

public static string ToString<T>(this List<T> list, char delimiter)
{
    return ToString(list, delimiter.ToString());
}

public static string ToString<T>(this List<T> list, string delimiter)
{
    ...
}

Also, the logic you are trying to implement can be easily achieved with:

string.Join(delimiter, list);

So you can basically delete all of those methods and just use that, unless you really want it as an extension method for lists.

Problem

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 ""; } ```

Original source