Are typical typical type parameters in C # valid in one form or another

I would like to create the following class fragment

class Lookup<TKey,TValue,TCollection>
where TCollection : ICollection<>
{
    public TCollection<TKey> _KeyCollection;
    public TCollection<TValue> _ValueCollection;
}

Is this pattern generally possible in C #? In the current form, he does not like the compiler. It seems you cannot hold back the type parameter as generic. However, it seems like a reasonable thing to do. Is there a trick to achieving this?

Note . This question is specifically about generics and type restrictions. He is not looking for work around what you think I'm trying to do in my broader application.

+4
source share
2 answers

, , , .

, , , , , .

, , 4.5 ( # 5.0):

. , . , , , (§4.4.2). , .

TCollection , .

+3

. :

class Lookup<TKey, TValue, TKeyCollection, TValueCollection>
    where TKeyCollection : ICollection<TKey>
    where TValueCollection : ICollection<TValue>
{
    public TKeyCollection _KeyCollection;
    public TValueCollection _ValueCollection;
}

, , .

+3

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


All Articles