How to pass a dictionary as a parameter when I do not need dictionary types?

Similar to this question:

Best way to convert a <string, string> dictionary into a single-line String representation?

But I want to ignore the types in the dictionary, since I plan to call the ToString () method for each key and value.

I think this should not be a problem, but I canโ€™t figure out how to go into an untyped dictionary without considering it just as an Object ... any ideas?

[EDIT] Add working code snippet: It works - thanks twoflower

public string DumpDictionary<TKey, TElement>(IDictionary<TKey, TElement> dictionary) { StringBuilder sb = new StringBuilder(); foreach (var v in dictionary) { sb.AppendLine(v.Key.ToString() + ":" + v.Value.ToString()); } return sb.ToString(); } 
+4
source share
1 answer

How about just

 void DumpDictionary<TKey, TElement>(IDictionary<TKey, TElement> dictionary) { ... } 

You can then call this without type arguments, since they will be output (in most cases):

 var dictionary = new Dictionary<long, MyClass>(); ... DumpDictionary(dictionary); 
+11
source

Source: https://habr.com/ru/post/1400477/


All Articles