tags:

views:

234

answers:

3

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

+5  A: 

"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"

http://msdn.microsoft.com/en-us/library/d5x73970.aspx

Sergio
+8  A: 

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 {/*...*/}
}
dtb
I'm always learning...
Martinho Fernandes
I've also never heard the term before, although I've already used this constraint type a couple of times.
dtb
+2  A: 

As an aside, it is bizarre to me that this somewhat salacious term managed to make it into the MSDN documentation. We certainly do not call these constraints "naked type constraints" on the C# compiler team and I was shocked, shocked! to discover a few years back that this is what the documentation said. We usually call them "type parameter constraints". I have no idea how this term got into the documentation in the first place; there's probably an interesting story there.

Eric Lippert