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. Will C# support primitive data type in Generics

Will C# support primitive data type in Generics

Scheduled Pinned Locked Moved C#
csharpdotnethelp
13 Posts 6 Posters 3 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.
  • D DaveyM69

    You can use struct

    public class MyClass<T> where T : struct
    {
    
    }
    

    or wrap an int in a (immutable?) class of your own and use that for the constraint.

    Dave
    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
    Why are you using VB6? Do you hate yourself? (Christian Graus)

    G Offline
    G Offline
    Gopal_Kanchana
    wrote on last edited by
    #3

    why this restriction. Allowing struct. what problem allowing int?

    D 1 Reply Last reply
    0
    • G Gopal_Kanchana

      Public class MyClass where T:int { } why it's giving error. why .net framework not supporting

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      public class MyClass<T>
      {
      ...
      }

      MyClass<int> myClass = new MyClass<int>();

      is this what you need ?

      G 1 Reply Last reply
      0
      • G Gopal_Kanchana

        why this restriction. Allowing struct. what problem allowing int?

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #5

        You'd have to ask Anders Hejlsberg[^]. It's the way generics were built into C#. Search for generic type constraints and you might find some reasoning. MSDN[^]

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        1 Reply Last reply
        0
        • G Gopal_Kanchana

          Public class MyClass where T:int { } why it's giving error. why .net framework not supporting

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #6

          What types do you expect you should be able to use with "int" as a constraint? You would only be able to use a single type - "int" itself (no uint, long etc. - those don't derive from int). But if there's only one possible type argument, it doesn't make sense to use generics at all!

          L 1 Reply Last reply
          0
          • D Daniel Grunwald

            What types do you expect you should be able to use with "int" as a constraint? You would only be able to use a single type - "int" itself (no uint, long etc. - those don't derive from int). But if there's only one possible type argument, it doesn't make sense to use generics at all!

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #7

            Hi, you might want to enumerate the acceptable types and use common features: public class MyClass<T> where T : int, long, float, double and now perform addition. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            D 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, you might want to enumerate the acceptable types and use common features: public class MyClass<T> where T : int, long, float, double and now perform addition. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              D Offline
              D Offline
              Daniel Grunwald
              wrote on last edited by
              #8

              But multiple constraints mean the type T must implement all of those. There is no way to say "int OR float" (which would have to automatically generate an interface with all methods common between int and float). But unfortunately, .NET doesn't support 'dynamic interfaces' (you declare an interface, and all classes having the appropriate methods will automatically implement it). Actually, I think VB10 will have something dynamic interfaces, but I guess it'll be implemented on top of Reflection/the DLR.

              L 1 Reply Last reply
              0
              • D Daniel Grunwald

                But multiple constraints mean the type T must implement all of those. There is no way to say "int OR float" (which would have to automatically generate an interface with all methods common between int and float). But unfortunately, .NET doesn't support 'dynamic interfaces' (you declare an interface, and all classes having the appropriate methods will automatically implement it). Actually, I think VB10 will have something dynamic interfaces, but I guess it'll be implemented on top of Reflection/the DLR.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #9

                Thanks. Syntax proposal: public class MyClass<T> where T in { int, long, float, double } :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                P 1 Reply Last reply
                0
                • L Luc Pattyn

                  Thanks. Syntax proposal: public class MyClass<T> where T in { int, long, float, double } :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #10

                  Right, that would be good. Or the numeric types should implement some sort of IDoMath interface and we could use that. I also want an enum constriant.

                  D 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Right, that would be good. Or the numeric types should implement some sort of IDoMath interface and we could use that. I also want an enum constriant.

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #11

                    If they made it so we had a Numeric constraint, or they implemented some common interface (INumeric), that would fix it for all the integral data types and enums too in one go.

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                    Why are you using VB6? Do you hate yourself? (Christian Graus)

                    P 1 Reply Last reply
                    0
                    • D DaveyM69

                      If they made it so we had a Numeric constraint, or they implemented some common interface (INumeric), that would fix it for all the integral data types and enums too in one go.

                      Dave
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                      Why are you using VB6? Do you hate yourself? (Christian Graus)

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #12

                      I want enums separate from the other types. I have generic classes that should accept enums only. As it stands, I have to check at runtime and throw and Exception. X|

                      1 Reply Last reply
                      0
                      • L Lost User

                        public class MyClass<T>
                        {
                        ...
                        }

                        MyClass<int> myClass = new MyClass<int>();

                        is this what you need ?

                        G Offline
                        G Offline
                        Gopal_Kanchana
                        wrote on last edited by
                        #13

                        public class CplusEnumExtension : whehre T:int { public CplusEnumExtension(int t) { enumT = t; } public bool Has(F findvalue) { return ((int)(object)enumT & (int)(object)findvalue)>0; } private int enumT=0; } In above code Has method i comparing int type that's why i expecting int from client side. Apartment from this I have implemented code below in C++ which client can use any type.(int, long etc) Is it possible in C# //EnumT->int,long,Ulong...0x0001; //Findvalue->ePDVE_DllError //usage : CEnumerationExtensions<ULONGLONG,ePileplandesign_verificationError> obj(errorcode); template ; class CEnumerationExtensions { public: CEnumerationExtensions(EnumT e) : enum_(e) {} public: bool Has(Findvalue value) { return ((enum_ & static_cast(value))>0); } private: EnumT enum_; };

                        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