Search code examples
c#genericstype-constraints

What does the term "Naked type constraint" refer to?


Recently I have read a term "naked type constraint" in the context of Generics. What does it mean? Where do we use it?


Solution

  • From MSDN:

    Constraint          Description
    
    where T : U         The type argument supplied for T must be or derive from
                        the argument supplied for U. This is called a naked type
                        constraint.
    

    When a generic type parameter is used as a constraint, it is called a naked type constraint. Naked type constraints are useful when a member function with its own type parameter has to constrain that parameter to the type parameter of the containing type, as shown in the following example:

    class List<T>
    {
        void Add<U>(List<U> items) where U : T {/*...*/}
    }