foreach 哈希表中的项目
foreach items in hashtable
我需要使用 Hastable
(不是 List
也不是 Dictionary
),而且我有很多带键的变量。我将键和变量添加到 class,并在我的程序中使用它。但是我不知道如何解析Hashtable
。我试过这个:
Hashtable toboofer = null;
string path = @"my.bin";
FileStream fin = File.OpenRead(path);
try
{
BinaryFormatter bf = new BinaryFormatter();
toboofer = (Hashtable)bf.Deserialize(fin);
for (int i = 0; i <= toboofer.Count; i++ )
//foreach (KeyValuePair<string, string> kvp in toboofer)
{
myclass cl = new myclass();
cl.Fio = toboofer[i].ToString();
cl.About = toboofer[i].ToString();
}
}
但是我有一个错误。当我尝试 string item
或循环 for
时,我也有错误。
Hashtable
有 DictionaryEntry
作为集合元素
foreach (DictionaryEntry entry in toboofer)
{
// do something
}
从哈希表中列出 myclass
:
var listOfMyClass = toboofer.Cast<DictionaryEntry>().
Select(e => new myclass()
{ Fio = e.Key.ToString(), About = e.Value.ToString() });
如果 DictionaryEntry 尝试使用此哈希表,其中 KeyValuePair generic 由通用字典 .Net 2(及更高版本)使用
另外请注意,Hashtable 没有它的通用版本,并且 hastable 中的每个元素都由 DictionaryEntry 表示
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
我需要使用 Hastable
(不是 List
也不是 Dictionary
),而且我有很多带键的变量。我将键和变量添加到 class,并在我的程序中使用它。但是我不知道如何解析Hashtable
。我试过这个:
Hashtable toboofer = null;
string path = @"my.bin";
FileStream fin = File.OpenRead(path);
try
{
BinaryFormatter bf = new BinaryFormatter();
toboofer = (Hashtable)bf.Deserialize(fin);
for (int i = 0; i <= toboofer.Count; i++ )
//foreach (KeyValuePair<string, string> kvp in toboofer)
{
myclass cl = new myclass();
cl.Fio = toboofer[i].ToString();
cl.About = toboofer[i].ToString();
}
}
但是我有一个错误。当我尝试 string item
或循环 for
时,我也有错误。
Hashtable
有 DictionaryEntry
作为集合元素
foreach (DictionaryEntry entry in toboofer)
{
// do something
}
从哈希表中列出 myclass
:
var listOfMyClass = toboofer.Cast<DictionaryEntry>().
Select(e => new myclass()
{ Fio = e.Key.ToString(), About = e.Value.ToString() });
如果 DictionaryEntry 尝试使用此哈希表,其中 KeyValuePair generic 由通用字典 .Net 2(及更高版本)使用
另外请注意,Hashtable 没有它的通用版本,并且 hastable 中的每个元素都由 DictionaryEntry 表示
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}