1

I have this question from Most efficient Dictionary.ToString() with formatting?, but my problem is if V is List, how to make it works. Now my solution is, change

itemString.AppendFormat(format, item.Key, item.Value);

to

itemString.AppendFormat(format, item.Key, item.Value.ToDelimitedStr());

here is code of ToDelimitedStr:

    public static string ToDelimitedStr<T>(this T source)
    {
        // List<string> 
        if (source is IList &&
            source.GetType().IsGenericType)
        {
            Type t1 = source.GetType().GetGenericArguments()[0];
            if (t1.Name == "String")
                ((IEnumerable<string>)source).ToDelimitedString();
        }
        return source.ToString();
    }

which only work for List<string>. How I can make it more generic? Also, I'm thinking, maybe I should not work on the top of

public string DictToString<T, V>(IEnumerable<KeyValuePair<T, V>> items, string format)

I should create a new version like

public string DictListToString<T, List<V>>(IEnumerable<KeyValuePair<T, List<V>>> items, string format)

how about that?

Thank you so much

Wes

Community
  • 1
  • 1
weslleywang
  • 569
  • 2
  • 8
  • 21
  • 1
    Are you asking how you can improve your `ToDelimitedStr` method, or how you can handle `Dictionary` when V can be anything, but when it happens to be an `IEnumerable` that it prints out every value? – adrianbanks Mar 31 '14 at 14:06
  • 1
    Don't write a generic method that just checks if the type is a `List` internally. If you want to do that, just have the parameter be a `List` instead. If you need to support a `Dictionary`, create a separate overload for it. – Servy Mar 31 '14 at 14:09
  • Hi adrianbanks, the later is my target. Improving ToDelimitedStr just the solution. At very beginning, I'm trying to find a way to override IEnumerable.ToString(), because the default version output like 'System.Collections.Generic.List`1[System.String]', but I like to prints out every value. – weslleywang Mar 31 '14 at 14:50

2 Answers2

0

Use string.Join

return string.Join("," , (IList<T>)source); 

I think it would be easier if you add an overloead for ToDelimitedStr which takes a IEnumerable<T> as parameter, then you don't need that type checks:

public static string ToDelimitedStr<T>(this IEnumerable<T> source)
{
     return string.Join(",", source);
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

use IEnumerable and just call ToString on the items:

public static string ToDelimitedStr<T>(this T source)
{
    // Will work for ANY IEnumerable
    if (source is IEnumerable)     // <----------------
    {
        IEnumerable<string> items =
           ((IEnumerable)source).OfType<object>()
                                .Select(o => o.ToString());
        // convert items to a single string here...
        return string.Join(", ", items);
    }
    return source.ToString();
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • @Selman22 There's not an overload that takes an `IEnumerable` - only `IEnumerable` and `IEnumerable`. In this case the underlying type of the collection is not known. It could be extracted via reflection but that's uneccessary in this case. – D Stanley Mar 31 '14 at 14:21