Re: Why is class Sample<T> where T : Stream, class ILLEGAL
Any 'class' or 'struct' constraints should come first but why are you adding
an 'all classes' constraints if you would like to use this generic class
with Stream and derivative classes?
--
Stanimir Stoyanov
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
On Mon, 03 Nov 2008 10:54:57 -0800, puzzlecracker <ironsel2000@gm ail.com>
wrote:
Would someone explain why this declaration is illegal: class Sample<T>
where T : Stream, class
Well, what does the error message say?
Here's a hint: what kind of type is "Stream"? Is there _any_ type you
could use with the "class" constraint that would either not be redundant
or contradictory?
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
new()
has to be the last constraint!
for multiple generic type you need to have the where clause for each
generic type. Like this
class Sample<T, Uwhere T: Stream where U:IDisposable
puzzlecracker wrote:
On Nov 3, 1:54 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
>
>Would someone explain why this declaration is illegal: class Sample<T>
>where T : Stream, class
>>
>
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).
>
class Sample<Twhere T : Stream, class
>
class Sample<Twhere T : new(), Stream
>
class Sample<T,Uwhere T : struct where U : class, T
>
class Sample<T,Uwhere T : Stream, U : IDisposable
>
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
On Nov 3, 2:05 pm, Ashutosh Bhawasinka <discuss...@ash utosh.inwrote:
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.
>
puzzlecracker wrote:
Would someone explain why this declaration is illegal: class Sample<T>
where T : Stream, class
that implies that struct, value types, cannot inherit from Reference
type... argh
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
On Mon, 03 Nov 2008 11:17:05 -0800, puzzlecracker <ironsel2000@gm ail.com>
wrote:
that implies that struct, value types, cannot inherit from Reference
type... argh
Well, sort of. Value types can and do inherit reference types. For
example, enumeration types inherit Enum, and all value types inherit
Object.
But yes...no matter what specific type you call out in the constraint,
adding "class" to the constraint will either be redundant (if that type is
a reference type) or contradictory (if that type is a value type).
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
Yes, value types (struct) can't inherit from either struct or class.
However they can (only) implement interfaces.
puzzlecracker wrote:
On Nov 3, 2:05 pm, Ashutosh Bhawasinka <discuss...@ash utosh.inwrote:
>
>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.
>>
>puzzlecracke r wrote:
>>
>>Would someone explain why this declaration is illegal: class Sample<T>
>>where T : Stream, class
>>>
>
that implies that struct, value types, cannot inherit from Reference
type... argh
>
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
Value type does *inherit *implicitly. But no *explicit *inheritance is
allowed.
Peter Duniho wrote:
On Mon, 03 Nov 2008 11:17:05 -0800, puzzlecracker
<ironsel2000@gm ail.comwrote:
>
>that implies that struct, value types, cannot inherit from Reference
>type... argh
>
Well, sort of. Value types can and do inherit reference types. For
example, enumeration types inherit Enum, and all value types inherit
Object.
>
But yes...no matter what specific type you call out in the constraint,
adding "class" to the constraint will either be redundant (if that
type is a reference type) or contradictory (if that type is a value
type).
>
Pete
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
On Nov 3, 7:02 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
Would someone explain why this declaration is illegal: class Sample<T>
where T : Stream, class
>
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).
<snip>
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 - see http://csharpindepth.com/ViewNote.aspx?NoteID=121
for details. Basically the constraint "class Sample<Twhere T :
class, Stream, new()" is listed as valid, but it isn't.
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
On Nov 4, 4:29 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
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.c om/ViewNote.aspx?N oteID=121
for details. Basically the constraint "class Sample<Twhere 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.
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
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.
Re: Why is class Sample<T> where T : Stream, class ILLEGAL
On Nov 4, 1:53 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
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 - see http://csharpindepth.com/ViewNote.aspx?NoteID=121
for details. Basically the constraint "class Sample<Twhere T :
class, Stream, new()" is listed as valid, but it isn't.
>
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.
Did you follow the link? I thought the note was pretty clear...
Comment