Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. FOR EXPERTS: Template Class: Type Constraints

FOR EXPERTS: Template Class: Type Constraints

Scheduled Pinned Locked Moved C#
helpcsharphtmlvisual-studiotutorial
6 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Axonn Echysttas
    wrote on last edited by
    #1

    Hey everybody... I got a question for advanced C# programmers. I've been playing around with type constraints for template classes, and I got stuck. I used [ and ] instead of < and > for template classes because I wanted to have HTML code for you to see better what code I got. In the C# 3.0 specification (and I will quote directly from Microsoft's document), it's said that this will work:

    class B[T] where T: IEnumerable {...}
    class D[T]: B[T] where T: IEnumerable {...}
    class E[T]: B[List[T]] {...}

    Because (so they say): "Since type parameters are not inherited, constraints are never inherited either. In the example below, D needs to specify the constraint on its type parameter T so that T satisfies the constraint imposed by the base class B[T]. In contrast, class E need not specify a constraint, because List[T] implements IEnumerable for any T." Now... I put this pre into VS 2008 and the compiler won't work. My pre:

    class B[T] where T : IEnumerable[T] { }
    class D[T] : B[T] where T : IEnumerable[T] { }
    class E[T] : B[List[T]] { }

    Error: "Error 1 The type 'System.Collections.Generic.List[T]' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'System.Collections.Generic.List[T]' to 'System.Collections.Generic.IEnumerable[System.Collections.Generic.List[T]]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 46 10 FunFollAdmin" Even more......... not even this works:

    B[test] test = new B[test]();

    Error: "Error 1 The type 'FunFollAdmin.frmMain.test' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'FunFollAdmin.frmMain.test' to 'System.Collections.Generic.IEnumerable[FunFollAdmin.frmMain.test]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 216 9 FunFollAdmin Error 2 The type 'FunFollAdmin.frmMain.test' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'FunFollAdmin.frmMain.test' to 'System.Collections.Generic.IEnumerable[FunFollAdmin.frmMain.test]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 216 28 FunFollAdmin" Even though I have implemented the class test correctly:

    class test : IEnumerable\[int\]
    {
      public test () { }
      #region IEnumerable\[int\] Memb
    
    M 1 Reply Last reply
    0
    • A Axonn Echysttas

      Hey everybody... I got a question for advanced C# programmers. I've been playing around with type constraints for template classes, and I got stuck. I used [ and ] instead of < and > for template classes because I wanted to have HTML code for you to see better what code I got. In the C# 3.0 specification (and I will quote directly from Microsoft's document), it's said that this will work:

      class B[T] where T: IEnumerable {...}
      class D[T]: B[T] where T: IEnumerable {...}
      class E[T]: B[List[T]] {...}

      Because (so they say): "Since type parameters are not inherited, constraints are never inherited either. In the example below, D needs to specify the constraint on its type parameter T so that T satisfies the constraint imposed by the base class B[T]. In contrast, class E need not specify a constraint, because List[T] implements IEnumerable for any T." Now... I put this pre into VS 2008 and the compiler won't work. My pre:

      class B[T] where T : IEnumerable[T] { }
      class D[T] : B[T] where T : IEnumerable[T] { }
      class E[T] : B[List[T]] { }

      Error: "Error 1 The type 'System.Collections.Generic.List[T]' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'System.Collections.Generic.List[T]' to 'System.Collections.Generic.IEnumerable[System.Collections.Generic.List[T]]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 46 10 FunFollAdmin" Even more......... not even this works:

      B[test] test = new B[test]();

      Error: "Error 1 The type 'FunFollAdmin.frmMain.test' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'FunFollAdmin.frmMain.test' to 'System.Collections.Generic.IEnumerable[FunFollAdmin.frmMain.test]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 216 9 FunFollAdmin Error 2 The type 'FunFollAdmin.frmMain.test' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'FunFollAdmin.frmMain.test' to 'System.Collections.Generic.IEnumerable[FunFollAdmin.frmMain.test]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 216 28 FunFollAdmin" Even though I have implemented the class test correctly:

      class test : IEnumerable\[int\]
      {
        public test () { }
        #region IEnumerable\[int\] Memb
      
      M Offline
      M Offline
      Mirko1980
      wrote on last edited by
      #2

      The compiler is right. You wrote where T : IEnumerable[T]: this means that T must be an enumeration of itself! Note that the MSDN sample uses the non-generic version of IEnumerable (where T : IEnumerable), meaning that T can be any enumeration.

      modified on Friday, February 6, 2009 10:10 AM

      A 1 Reply Last reply
      0
      • M Mirko1980

        The compiler is right. You wrote where T : IEnumerable[T]: this means that T must be an enumeration of itself! Note that the MSDN sample uses the non-generic version of IEnumerable (where T : IEnumerable), meaning that T can be any enumeration.

        modified on Friday, February 6, 2009 10:10 AM

        A Offline
        A Offline
        Axonn Echysttas
        wrote on last edited by
        #3

        Note that the MSDN sample uses the non-generic version of IEnumerable (where T : IEnumerable[T]), meaning that T can be any enumeration.

        You mean where T : IEnumerable Ok but how do you declare it so? Because if I declare it so, it says Error 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 48 33 FunFollAdmin I used the code directly from MSDN interface IMyInterface { } class Dictionary[TKey, TVal] where TKey : IComparable, IEnumerable where TVal : IMyInterface { public void Add (TKey key, TVal val) { } } Same error as above!!! I don't get it! Oh boy......... ::- ( Error 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 48 33 FunFollAdmin

        -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?

        M 1 Reply Last reply
        0
        • A Axonn Echysttas

          Note that the MSDN sample uses the non-generic version of IEnumerable (where T : IEnumerable[T]), meaning that T can be any enumeration.

          You mean where T : IEnumerable Ok but how do you declare it so? Because if I declare it so, it says Error 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 48 33 FunFollAdmin I used the code directly from MSDN interface IMyInterface { } class Dictionary[TKey, TVal] where TKey : IComparable, IEnumerable where TVal : IMyInterface { public void Add (TKey key, TVal val) { } } Same error as above!!! I don't get it! Oh boy......... ::- ( Error 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 48 33 FunFollAdmin

          -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?

          M Offline
          M Offline
          Mirko1980
          wrote on last edited by
          #4

          You mean where T : IEnumerable

          Yes, I mistyped. Corrected now. You must add using System.Collections in order to use the non-generic version of IEnumerable (or you use its full name: where T : System.Collections.IEnumerable).

          A 1 Reply Last reply
          0
          • M Mirko1980

            You mean where T : IEnumerable

            Yes, I mistyped. Corrected now. You must add using System.Collections in order to use the non-generic version of IEnumerable (or you use its full name: where T : System.Collections.IEnumerable).

            A Offline
            A Offline
            Axonn Echysttas
            wrote on last edited by
            #5

            Ah, damn. I had using System.Collections.Generic; but not using System.Collections; Thank you Mirko! ::- ) That fixed it.

            -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?

            M 1 Reply Last reply
            0
            • A Axonn Echysttas

              Ah, damn. I had using System.Collections.Generic; but not using System.Collections; Thank you Mirko! ::- ) That fixed it.

              -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?

              M Offline
              M Offline
              Mirko1980
              wrote on last edited by
              #6

              You welcome :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups