C# Language: generics, open/closed, bound/unbound, constructed

后端 未结 2 843
日久生厌
日久生厌 2020-12-02 08:44

I\'m reading book \"the C# programming Language\", 4th Edition, by Anders Hejlsberg etc.

There are several definitions that are a bit twisting:

unbo

相关标签:
2条回答
  • 2020-12-02 09:11

    An excellent answer is given by Jon, here.

    Not giving a technical description since everything is there perfectly in the linked answer. To merely replicate the gist of it as an answer here, it would look like:

    A                             //non generic, bound
    A<U, V>                       //generic,     bound,  open,   constructed
    A<int, V>                     //generic,     bound,  open,   constructed
    A<int, int>                   //generic,     bound,  closed, constructed
    A<,> (used like typeof(A<,>)) //generic,     unbound 
    

    Edited after discussion with Heinzi.

    0 讨论(0)
  • 2020-12-02 09:12

    These are examples of unbound generic types:

    • List<>
    • Dictionary<,>

    They can be used with typeof, i.e., the following are valid expressions:

    • typeof(List<>)
    • typeof(Dictionary<,>)

    That should answer your question 2. With respect to question 1, note that type arguments can be constructed types or type parameters. Thus, your list should be updated as follows:

    public class MyClass<T, U> {  // declares the type parameters T and U
    
        // all of these are
        // - generic,
        // - constructed (since two type arguments are supplied), and
        // - bound (since they are constructed):
    
        private Dictionary<T, U> var1;     // open (since T and U are type parameters)
        private Dictionary<T, int> var2;   // open (since T is a type parameter)
        private Dictionary<int, int> var3; // closed
    }
    
    0 讨论(0)
提交回复
热议问题