Why is class Sample<T> where T : Stream, class ILLEGAL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    Why is class Sample<T> where T : Stream, class ILLEGAL

    Would someone explain why this declaration is illegal: class Sample<T>
    where T : Stream, class
  • puzzlecracker

    #2
    Re: Why is class Sample&lt;T&gt; where T : Stream, class ILLEGAL

    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

    Comment

    • Ashutosh Bhawasinka

      #3
      Re: Why is class Sample&lt;T&gt; where T : Stream, class ILLEGAL

      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
      >

      Comment

      • Stanimir Stoyanov

        #4
        Re: Why is class Sample&lt;T&gt; 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
        메이저사이트 순위 먹튀검증이 완료된 토토사이트 커뮤니티 - 토토핫 입니다. 저희 토토핫에서는 베팅할때 필수로 점검해야 되는 안전 가이드를 제공하며 안전한 메이저 놀이터 목록과 메이저사이트 먹튀검증 꽁머니사이트를 추천드립니다.


        "puzzlecrac ker" <ironsel2000@gm ail.comwrote in message
        news:453a59ab-d472-43ae-98b2-9a96ee71b0a5@z6 g2000pre.google groups.com...
        Would someone explain why this declaration is illegal: class Sample<T>
        where T : Stream, class

        Comment

        • Ashutosh Bhawasinka

          #5
          Re: Why is class Sample&lt;T&gt; where T : Stream, class ILLEGAL

          Also, restriction by value/reference type has to be the first
          constraint. And restriction of new has to be the last.

          In this case you will get the error saying that it should be the first
          constraint (similar message). If you change it to

          class Sample<T>
          where T : class,Stream

          then my previous comment applies.

          puzzlecracker wrote:
          Would someone explain why this declaration is illegal: class Sample<T>
          where T : Stream, class
          >

          Comment

          • Peter Duniho

            #6
            Re: Why is class Sample&lt;T&gt; 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?

            Comment

            • Ashutosh Bhawasinka

              #7
              Re: Why is class Sample&lt;T&gt; 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
              >

              Comment

              • puzzlecracker

                #8
                Re: Why is class Sample&lt;T&gt; 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

                Comment

                • Peter Duniho

                  #9
                  Re: Why is class Sample&lt;T&gt; 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).

                  Pete

                  Comment

                  • Ashutosh Bhawasinka

                    #10
                    Re: Why is class Sample&lt;T&gt; 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
                    >

                    Comment

                    • Ashutosh Bhawasinka

                      #11
                      Re: Why is class Sample&lt;T&gt; 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

                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: Why is class Sample&lt;T&gt; 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.

                        Jon

                        Comment

                        • puzzlecracker

                          #13
                          Re: Why is class Sample&lt;T&gt; 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.

                          Thanks

                          Comment

                          • Marc Gravell

                            #14
                            Re: Why is class Sample&lt;T&gt; 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.

                            Marc

                            Comment

                            • Jon Skeet [C# MVP]

                              #15
                              Re: Why is class Sample&lt;T&gt; 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...

                              Jon

                              Comment

                              Working...