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. Multiple Generic Constraint Syntax?

Multiple Generic Constraint Syntax?

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 0 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.
  • M Offline
    M Offline
    mike montagne
    wrote on last edited by
    #1

    Is there a legitimate syntax for declaring a generic class taking multiple generic parameters, specifying constraints on *each* parameter? For instance, VS2005 will not complain about multiple constraint parameters and a *singular* constraint (where) expression:

    public class MyGeneric<T1, T2> : Component where T1 : Class1
         {
         }
    

    But it doesn't want to see the following:

    public class MyGeneric<T1, T2> : Component where T1 : Class1, where T2 : Class2
         {
         }
    

    Does anybody know the syntax for multiple generic constraints? TIA, m

    D 1 Reply Last reply
    0
    • M mike montagne

      Is there a legitimate syntax for declaring a generic class taking multiple generic parameters, specifying constraints on *each* parameter? For instance, VS2005 will not complain about multiple constraint parameters and a *singular* constraint (where) expression:

      public class MyGeneric<T1, T2> : Component where T1 : Class1
           {
           }
      

      But it doesn't want to see the following:

      public class MyGeneric<T1, T2> : Component where T1 : Class1, where T2 : Class2
           {
           }
      

      Does anybody know the syntax for multiple generic constraints? TIA, m

      D Offline
      D Offline
      Dave Doknjas
      wrote on last edited by
      #2

      Just omit the comma: public class MyGeneric : Component where T1 : Class1 where T2 : Class2

      David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter, VB to C++ converter Instant Python: C# to Python converter, VB to Python converter

      M 1 Reply Last reply
      0
      • D Dave Doknjas

        Just omit the comma: public class MyGeneric : Component where T1 : Class1 where T2 : Class2

        David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter, VB to C++ converter Instant Python: C# to Python converter, VB to Python converter

        M Offline
        M Offline
        mike montagne
        wrote on last edited by
        #3

        Wow, thanks! Funny, you have to comma delimit the new constraint after another constraint... public class MyCollection<T1, T2> : Component where T1: MyClass1, new() where T2 : MyClass2 ...but not the where of the subsequent constraint? :-) Sure does work. I appreciate the tip. m

        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