我正在审查某人的代码并遇到了这个私人课程:
class CustomType : Dictionary<int, SomeOtherCustomType>
{
// This is empty; nothing omitted here
}
Run Code Online (Sandbox Code Playgroud)
然后在整个父类中使用CustomType.这当然很整洁,因为CustomType比
Dictionary<int, SomeOtherCustomType>
Run Code Online (Sandbox Code Playgroud)
我的问题是,为快捷方式设置内部类的性能/内存含义是什么?在性能敏感的应用程序中,这是否有助于(甚至轻微)更高的内存和/或CPU使用率?
Rei*_*ica 50
除非有另一个定义自定义类型的原因,否则我建议将其更改为using语句.
using CustomType = Dictionary<int, SomeOtherCustomType>;
Run Code Online (Sandbox Code Playgroud)
它只是为字典定义了一个别名,如果你正在使用一些复杂的参数化类,它们会非常有用.
这消除了声明新类型的需要,因为当代码站立时,以下将失败.
CustomType dictionary = new Dictionary<int, SomeOtherCustomType>(); //custom type is a different class and can't be instantiated with a dictionary
Run Code Online (Sandbox Code Playgroud)
但是,如果您使用别名,它将起作用.
Mik*_*oud 15
它的短期和长期是,没有.该类CustomType根据定义a Dictionary<int, SomeOtherCustomType>,因此将按此分配.
这尤其正确,因为实际上没有该类的实现.