C# 如何循环使用类对象外接程序哈希表对象

C# 如何循环使用类对象外接程序哈希表对象,c#,C#,我得到了这个错误: HashTableDemo.exe中发生类型为“System.InvalidCastException”的未处理异常 试一试,因为hash-tbale中的数据存储不是您得到错误的Friend对象的类型,如果您不知道类型,那么使用var将解决错误 // this is my class object Friend f1 = new Friend("Nazir",24); Friend f2 = new Friend("Hamza", 24); Friend f3 = new F

我得到了这个错误:

HashTableDemo.exe中发生类型为“System.InvalidCastException”的未处理异常


试一试,因为hash-tbale中的数据存储不是您得到错误的
Friend
对象的类型,如果您不知道类型,那么使用
var
将解决错误

// this is my class object
Friend f1 = new Friend("Nazir",24);
Friend f2 = new Friend("Hamza", 24);
Friend f3 = new Friend("Abdullah", 23);

Hashtable myhash = new Hashtable();

// add this object in hashtable object
myhash.Add("13b-049-bs",f1);
myhash.Add("13b-034-bs", f1);
myhash.Add("13b-038-bs", f1);

foreach (Friend item in myhash)
{
    Console.WriteLine("Key {0}\tName {1}\tAge {2}",item.Name,item.Age,myhash.Keys);
}
或者你可以这样做

  foreach (var  item in myhash)
        {
           string key = de.Key.ToString();
           Friend val  = (Friend) item.Value;
           Console.WriteLine("Key {0}\tName {1}\tAge {2}", key, val.Name, val.Age );
        }
读取:

哈希表的迭代器(枚举器)返回类型为的对象。因此,在
foreach
循环中,每个项都有两个属性:
Key
Value
,这两个属性的类型都是
object
。您可以将每个项目上的
属性分别强制转换为所需的类型。例如:

foreach(DictionaryEntry de in myHashtable)
{
      string key = de.Key.ToString();
   Friend val  = (Friend) de.Value;
   Console.WriteLine("Key {0}\tName {1}\tAge {2}", key, val.Name, val.Age );
}

使用强类型词典(
使用System.Collections.Generic
需要):

使用System.Collections.Generic;
公共课程
{
班友
{
公共字符串名称{get;set;}
公共整数{get;set;}
公共好友(字符串名称,整数年龄)
{
名称=名称;
年龄=年龄;
}
}
公共静态void Main()
{
//这是我的类对象
朋友f1=新朋友(“纳齐尔”,24);
Friend f2=新朋友(“Hamza”,24);
Friend f3=新朋友(“阿卜杜拉”,23岁);
var myDict=新字典();
//将此对象添加到哈希表对象中
myDict[“13b-049-bs”]=f1;
myDict[“13b-034-bs”]=f1;
myDict[“13b-038-bs”]=f1;
foreach(myDict中的KeyValuePair项)
{
WriteLine(“Key{0}\tName{1}\tAge{2}”、item.Key、item.Value.Name、item.Value.Age);
}
}
}

强类型的优点是,您永远不会放置
敌人
(或本条中的其他类型-除非它来自于
Friend

@Rafalon-为什么它不是好的做法…这是根据语言结构的法律方式…它与linq一起引入仅用于此目的…任何我用字典更新答案的方式,希望你喜欢回复你-1point@KonstantinChernov-更新了part也…………我还没看到……谢谢你指点out@Rafalon-关于使用或不使用它的争论很多次,这取决于开发人员,我的观点是,在我的编码风格中,我使用var…………为什么不使用lagnaguge是为了提供该功能……我没有添加min Theerror 1'object'不包含'Key'和no'的定义可以找到接受类型为“object”的第一个参数的扩展方法“Key”(是否缺少using指令或程序集引用?)E:\C sharp\UnderstandingCollection\HashTableDemo\Program.cs 26 35 HashTableDemo请等待5分钟进行编辑,我正在尝试编辑您的问题5分钟,但您取消了4次
xD
我对语言和代码格式做了一些改进
:)
为什么要使用非类型哈希表?使用强类型的
字典会更容易些-这样您就不必强制转换键或值。迭代它也可以工作-您将通过
foreach获得stronly-typed
KeyValuePair
(yourDict中的var kvp)
然后可以通过
kvp.Key
kvp.Value
访问,而无需强制转换。。。
foreach (DictionaryEntry item in myhash)
{
    // Cast item.Key to string
    string key = (string)item.Key;

    // And cast item.Value to Friend
    Friend friend = (Friend)item.Value;

    Console.WriteLine("Key {0}\tName {1}\tAge {2}", key, friend.Name, friend.Age);
}
using System.Collections.Generic;

public class Program
{
    class Friend
    {
        public string Name {get;set;}    
        public int Age {get;set;}

        public Friend(string name, int age)
        {
            Name = name;
            Age = age;
        }
    }

    public static void Main()
    {
        // this is my class object
        Friend f1 = new Friend("Nazir", 24);
        Friend f2 = new Friend("Hamza", 24);
        Friend f3 = new Friend("Abdullah", 23);
        var myDict = new Dictionary<string, Friend>();
        // add this object in hashtable object
        myDict["13b-049-bs"] = f1;
        myDict["13b-034-bs"] = f1;
        myDict["13b-038-bs"] = f1;
        foreach (KeyValuePair<string, Friend> item in myDict)
        {
            Console.WriteLine("Key {0}\tName {1}\tAge {2}", item.Key, item.Value.Name, item.Value.Age);
        }
    }
}