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

Is there any way to encapsulate a dictionary as a new type, such as a DataDictionary, so that instead of changing the definition in many places that it uses, it can only be changed in a few. Or should I just wrap this in another class that provides only the aspects I need?

+3
source share
2 answers

The dictionary is not sealed, so if you need the appropriate subtype, do

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

And one more option:

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

}

This gives you more freedom in your own type of design.
And if you meant "How to eliminate type parameters", use something like:

class DataDictionary : Dictionary<string, int>
{
}
+4

.

using DataDictionary = Dictionary<int,int>

DataDictionary , .

+1

Source: https://habr.com/ru/post/1720573/


All Articles