在我的应用程序开发中,我断定我需要这个变量。
IDictionary<string, IDictionary<Levels, IList<Problem>>> PackageDictionaryForProblems
IDictionary<string, IDictionary<Levels, IList<ProblemRule>>> PackageDictionaryForProblemRules
但是因为我需要访问字典的每个键,所以这很痛苦。我认为创建一个类以避免写入所有数据类型是一个不错的选择:
public class PackageDictionaryForProblems : IDictionary<string, IDictionary<Levels, IList<Problem>>>
{ }
// the sane for the second dictionary
你怎么看这件事?只创建此类是一个好习惯吗?还是我需要为每个类创建多个类?即
public class ProblemCollection : IList<Problem>
{ }
最佳答案
定义多级集合的两个选项在运行类型上没有区别,如
IDictionary<string, IDictionary<Levels, IList<Problem>>> PackageDictionaryForProblems
或定义/使用类似
的类型public class PackageDictionaryForProblems : IDictionary<string, IDictionary<Levels, IList<Problem>>>
{ }
在 PackageDictionaryForProblems
中没有任何进一步的功能, 它只是作为 IDictionary<string, IDictionary<Levels, IList<Problem>>>
的别名.
您最终仍会得到相同的内存分配、访问时间等。添加该自定义类型只会简化代码的可读性(或者不会……我个人认为查看 IDictionary<string, IDictionary<Levels, IList<Problem>>>
定义更容易理解数据结构。这只是个人喜好)。
关于c# - 如果我有冗长的数据类型会有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11727571/