【发布时间】:2011-11-09 05:41:14
【问题描述】:
我编写了一些代码来将任何IEnumerable 集合输出到文件,但不能将字典传递给它。我现在正在尝试将字典(可能是 int、int 或 int、string 或任何其他组合)转换为数组,以便我可以这样做。当我尝试将其传递给需要IEnumerable 的方法时,下面的代码表示错误。
泛型是我没有做太多的事情,所以也许我做错了什么。
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;
}
我正在呼叫的另一个人:
public static bool TypedListToFile<T>(IEnumerable<T> TypedList, string FilePath)
【问题讨论】:
-
将字典传递给这个方法有什么问题? Dictionary
类实现 IEnumerable > -
感谢 Makkam - 我也有同样的想法(尽管尝试过 Dictionary 而不是 KeyValuePair)。你说的对!如果您想将此作为答案,我会打勾:)
标签: c# generics type-conversion