有两个键和一个对象的最佳C#集合是什么?
我有一个MenuManager类,每个模块可以添加一个键和要加载到主要内容中的元素:
private Dictionary _mainContentItems = new Dictionary(); public Dictionary MainContentItems { get { return _mainContentItems; } set { _mainContentItems = value; } }
所以客户模块注册它的视图如下:
layoutManager.MainContentViews.Add("customer-help", this.container.Resolve()); layoutManager.MainContentViews.Add("customer-main", this.container.Resolve());
所以后来我在前面带了一个特定的视图我说:
layoutManager.ShowMainContentView("customer-help");
为了获得默认视图(第一个注册视图),我说:
layoutManager.ShowDefaultView("customer");
这很好用。
但是,我想用连字符消除“代码味道”,它将模块名称和视图名称分开,所以我想注册这个命令:
layoutManager.MainContentViews.Add("customer","help", this.container.Resolve());
但是更换我当前词典的最佳方法是什么,例如我想到的是:
-
Dictionary (doesn't exist)
-
Dictionary<KeyValuePair, object>
-
Dictionary
新的集合需要能够做到这一点:
- 获取带模块和视图键的视图(例如“customer”,“help”返回1视图)
- 按模块键获取所有视图的集合(例如“customer”返回5个视图)
严格符合您的标准,使用Dictionary
;
var dict = new Dictionary>(); ... object view = dict["customer"]["help"]; Dictionary.ValueCollection views = dict["customer"].Values;
正如在类似的线程中提到的,在.NET 4中表示2键字典的好方法是使用Tuple类:
IDictionary, V>
您所描述的内容听起来像是字典中的复合键,而不是两个键。 我建议设置一个简单的结构来表示这个键:
struct Section { string Area { get; set; } string Area2 { get; set; } // override ToHashCode, Equals and implement IComparable. }