简体   繁体   中英

How to encapsulate the concept of a combination of generics into a new type?

Is there any way that one can encapsulate Dictionary to be a new type like DataDictionary so that instead of needing to change the definition in however many places it is used, it can be changed in only a few. Or should I just wrap this in another class that exposes only the aspects that I need?

Dictionary is not sealed so if you want a proper sub-type, do

class DataDictionary<K, V> : Dictionary<K,V>
{
}

And another option is :

class DataDictionary<K, V> 
{
   private Dictionary<K,V> _data;

}

Which gives you more freedom to design your own type.
And if you meant "How to eliminate the type-parameters", use something like:

class DataDictionary : Dictionary<string, int>
{
}

You can use using directive on the top of your code file.

using DataDictionary = Dictionary<int,int>

But if you use this DataDictionary in a lot of code files, encapsulation or inheritance are much more preferred.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM