Invalid type constraint listed as valid

Chapter 3: Parameterized typing with generics: P 79

Created: 6/6/2008
Last updated: 8/15/2010
Severity: Technical error

On page 79, I include the following constraint as a legal one:

// Not valid after all!
class Sample<T> where T : class, Stream, new()

This is in fact not a valid constraint, for a reason I wasn't aware of:

If you have a derivation type constraint where the type is a class (such as Stream) you can't also specify either class or struct (the reference and value type constraints respectively).

For any class other than Object, ValueType and Enum, any derived classes will be reference types anyway, so the constraint doesn't do you any good. Note that you can specify an interface for the derivation type constraint and still specify class/struct. For example:

// This is valid though
class Sample<T> where T : class, IDisposable, new()

For the next printing of the book, we will simply change the incorrect line to the valid example directly above - there isn't enough room to explain why the top version is invalid without reflowing the page - but if there's ever a comprehensive update to the book, I'll add a full explanation there.