Hashtable具有单个键的不同值

2 c# mono hashtable monodevelop ios-simulator

如果我有相同键的多个值,那么如何在哈希表中添加它,例如:

                   Hashtable hs = new Hashtable();
                      hs["id"]= "x001.xhtml";
                      hs["media-type"]= "application/xhtm+xml"; 
                      hs["href"]= "text/001.xhtml";

                      hs["id"]= "x002.xhtml";
                      hs["media-type"]= "application/xhtm+xml";
                      hs["href"]= "text/002.xhtml";

                     hs["id"]= "x003.xhtml";
                     hs["media-type"]= "application/xhtm+xml";
                     hs["href"]= "text/003.xhtml";
Run Code Online (Sandbox Code Playgroud)

这只是哈希表中的最后一组,即

                     hs["id"]= "x003.xhtml";
                     hs["media-type"]= "application/xhtm+xml";
                     hs["href"]= "text/003.xhtml";
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题....提前感谢!!

Mar*_*ell 9

哈希表定义为具有唯一键,索引器替换现有值.听起来你真的想要一个对象列表,即

var list = new List<YourType> {
    new YourType { Id = "x001.xhtml", MediaType = "...", Href = "..." },
    new YourType { Id = "x002.xhtml", MediaType = "...", Href = "..." },
    new YourType { Id = "x003.xhtml", MediaType = "...", Href = "..." }
};
Run Code Online (Sandbox Code Playgroud)

public class YourType {
    public string Id {get;set;}
    public string MediaType {get;set;}
    public string Href {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

(或类似的)

  • 在我还在打字的时候,看到你提出一个完整的答案并收集upvotes是非常令人沮丧的.不过还是+1. (2认同)
  • @skolima我会努力打字慢.抱歉. (2认同)