I’ve written some code to output any IEnumerable collection to a file, but can’t pass dictionaries to it. I’m now trying to convert the dictionary (which might be int, int or int, string or any other combination) into an array so I can do this. The code below indicates an error when I try to pass it to the method requiring the IEnumerable.
Generics are something I haven’t done much with so perhaps I’ve done something wrong with those.
public static bool DictionaryToFile<T, U>(Dictionary<T, U> TheDictionary, string FilePath)
{
long count = 0;
string[,] myArray = new string[2,TheDictionary.Count];
foreach (var current in TheDictionary)
{
myArray[0, count] = current.Key.ToString();
myArray[1, count] = current.Value.ToString();
}
// error appears here
TypedListToFile<string[,]>(myArray, FilePath);
return true;
}
The other one I’m calling:
public static bool TypedListToFile<T>(IEnumerable<T> TypedList, string FilePath)
It really depends on what you are trying to do: when you read: Why do C# Multidimensional arrays not implement IEnumerable<T>? You’ll see that a multidimensional array doesn’t implement IEnumerable, so you have to convert it first. Yet, your code doesn’t make sense. I assume you need to increment count in your loop?
Now as for solutions: you can simulate VB behaviour which automatically converts multi-dimensional arrays into enumerables by applying a linq query over it. like: