3

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 "";
    }
12
  • Is it just me or your ToString method calls itself indefinitely?
    – Asik
    Commented May 13, 2012 at 4:44
  • Which is the type of the tokens variable?
    – Ivo
    Commented May 13, 2012 at 4:56
  • See edit for my answer, as followup on your edit. Commented May 13, 2012 at 5:01
  • 1
    the last ToString should really use a StringBuilder instead of appending the list items to a local string... Commented May 13, 2012 at 5:05
  • @MikeCorcoran It should actually use string.Join... Which apparently is just as fast as StringBuilder, given you already have all the substrings in an IEnumerable of some sort. Commented May 13, 2012 at 5:11

2 Answers 2

11

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.

7
  • I'm not exactly sure how to do what you're describing. I guess I'm somewhat of a newb. I tried: return ListExtensions.ToString(tokens, delimiter); and it will display the char overload, but it doesn't like tokens for some reason... Commented May 13, 2012 at 4:47
  • @Thick_propheT That is probably because you didn't make the methods generic, meaning it expects List<object> - which tokens isn't. Try using the two generic methods at the end of the answer. Commented May 13, 2012 at 4:52
  • You're right, I tried: return ListExtension.ToString(new List<object>(), delimiter); and it was 110% satisfied XD Commented May 13, 2012 at 5:01
  • @Thick_propheT That would create a new and empty list... Are you sure that's the code you've used? Commented May 13, 2012 at 5:04
  • Oh, no, it's definitely not what I need, I was just trying to see what it would take to satisfy visual studio. Commented May 13, 2012 at 5:07
1

I think your problem is that you are specifying the type of object for your generic List and not making it a generic method.

See if it works when you define something like the following:

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

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

Your original function with the string delimiter was just calling itself so you'll have to change your ToString<T>(this List<T> list, string delimiter) to do something useful here like a String.join

3
  • This will result with endless and useless recursion. Commented May 13, 2012 at 4:48
  • 1
    Haha yes, I see that now. I don't actually know what original poster was trying for in the string extension method. I guess there's a reason they call this stackoverflow
    – nvuono
    Commented May 13, 2012 at 4:50
  • The fact he wants a string and uses a list and delimiter kind of narrows down the options. Commented May 13, 2012 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.