如果我对同一个键有多个值,那么如何将它添加到哈希表中 例如:
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";
这只取哈希表中的最后一组,即
hs["id"]= "x003.xhtml";
hs["media-type"]= "application/xhtm+xml";
hs["href"]= "text/003.xhtml";
如何解决这个问题....提前致谢!!
最佳答案
哈希表被定义为具有唯一键,索引器替换现有值。听起来您实际上想要一个对象列表,即
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 = "..." }
};
与
public class YourType {
public string Id {get;set;}
public string MediaType {get;set;}
public string Href {get;set;}
}
(或类似的)
关于c# - 单键具有不同值的哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846215/