P
puzzlecracker
Would someone explain why this declaration is illegal: class Sample<T>
where T : Stream, class
where T : Stream, class
Would someone explain why this declaration is illegal: class Sample<T>
where T : Stream, class
You can restrict the generic implementation by either a base type or
value/reference type. Not both.
Stream is already a class. So, T will be a reference type (class) if you
restrict T to be a Stream. So, basically you are trying to say class 2
times.
They are taken from C# In depth book
And a few more that are also illegal, and I don't understand why
(compiler is less than helpful).
There have been lots of good answers - but I'd just like to point out
one problem with this section of the book. There is a genuine
technical error - seehttp://csharpindepth.com/ViewNote.aspx?NoteID=121
for details. Basically the constraint "class Sample<T> where T :
class, Stream, new()" is listed as valid, but it isn't.
Jon
Why would it be invalid? All it's saying that the close type ofThere have been lots of good answers - but I'd just like to point out
one problem with this section of the book. There is a genuine
technical error - seehttp://csharpindepth.com/ViewNote.aspx?NoteID=121
for details. Basically the constraint "class Sample<T> where T :
class, Stream, new()" is listed as valid, but it isn't.
Jon
Why would it be invalid? All it's saying that the close type of
instance is required to be class, inherit from Stream and have
parameterless constructor.
You are only allows one primary constraint; both ": class" and ":
Stream" are classed as primary constraints. If you think about it, this
is logical: Stream is a class, so if T : Stream, T *must* be a class.
Marc
Why would it be invalid? All it's saying that the close type of
instance is required to be class, inherit from Stream and have
parameterless constructor.
where T: class states this about T; "Can be any class", it _doesn't_ state
"Can't be a struct" or "must at least be a class".
where T: Stream states this about T; "Must be a Stream".
Thus where T: class, Stream states this about T;
"Can be any class" and "Must be a Stream". Clearly this is contradition,
since not all classes are Streams.
where T: class states this about T; "Can be any class", it _doesn't_ state
"Can't be a struct" or "must at least be a class".
where T: Stream states this about T; "Must be a Stream".
Thus where T: class, Stream states this about T;
"Can be any class" and "Must be a Stream". Clearly this is contradition,
since not all classes are Streams.
where T: class states this about T; "Can be any class", it _doesn't_ state
"Can't be a struct" or "must at least be a class".
where T: Stream states this about T; "Must be a Stream".
Thus where T: class, Stream states this about T;
"Can be any class" and "Must be a Stream". Clearly this is contradition,
since not all classes are Streams.
where T: class states this about T; "Can be any class", it _doesn't_ state
"Can't be a struct" or "must at least be a class".
where T: Stream states this about T; "Must be a Stream".
Thus where T: class, Stream states this about T;
"Can be any class" and "Must be a Stream". Clearly this is contradition,
since not all classes are Streams.