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. is it possible to use a value type in a generic method constraint?

is it possible to use a value type in a generic method constraint?

Scheduled Pinned Locked Moved C#
question
7 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
    michal kreslik
    wrote on last edited by
    #1

    Hello, how can I use a value type in a generic method constraint? Is it at all possible? Obviously, public static T1 GenericMethod(T1 input) where T1: int, double, decimal won't work. Thanks, Michal

    J 1 Reply Last reply
    0
    • M michal kreslik

      Hello, how can I use a value type in a generic method constraint? Is it at all possible? Obviously, public static T1 GenericMethod(T1 input) where T1: int, double, decimal won't work. Thanks, Michal

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      public static T1 GenericMethod(T1 input)
      where T1 : struct
      {
      }

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Christian Zionism, as seen from a Jewish perspective The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      M 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        public static T1 GenericMethod(T1 input)
        where T1 : struct
        {
        }

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Christian Zionism, as seen from a Jewish perspective The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        M Offline
        M Offline
        michal kreslik
        wrote on last edited by
        #3

        Ok, thanks, but I assume that all value types are comparable, however the following example code doesn't compile with "where T1: struct": public static T1 GenericMethod(T1 value1, T1 value2) where T1: struct { if (value1 > value2) { return value1; } return value2; } So there must be some other way to restrict T1 just to value types. Thanks, Michal

        J 1 Reply Last reply
        0
        • M michal kreslik

          Ok, thanks, but I assume that all value types are comparable, however the following example code doesn't compile with "where T1: struct": public static T1 GenericMethod(T1 value1, T1 value2) where T1: struct { if (value1 > value2) { return value1; } return value2; } So there must be some other way to restrict T1 just to value types. Thanks, Michal

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          How about this?

          public static T1 GenericMethod<T1>(IComparable<T1> value, IComparable<T1> value2)
          where T1 : struct
          {
          }

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Christian Zionism, as seen from a Jewish perspective The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          M 1 Reply Last reply
          0
          • J Judah Gabriel Himango

            How about this?

            public static T1 GenericMethod<T1>(IComparable<T1> value, IComparable<T1> value2)
            where T1 : struct
            {
            }

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Christian Zionism, as seen from a Jewish perspective The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            M Offline
            M Offline
            michal kreslik
            wrote on last edited by
            #5

            Doesn't work, unfortunately: public static T1 GenericMethod(IComparable value1, IComparable value2) where T1 : struct { if (value1 > value2) { return value1; } return value2; } is producing this error: Operator '>' cannot be applied to operands of type 'System.IComparable' and 'System.IComparable' It doesn't help to use "where T1: struct, IComparable" neither. Michal

            J 1 Reply Last reply
            0
            • M michal kreslik

              Doesn't work, unfortunately: public static T1 GenericMethod(IComparable value1, IComparable value2) where T1 : struct { if (value1 > value2) { return value1; } return value2; } is producing this error: Operator '>' cannot be applied to operands of type 'System.IComparable' and 'System.IComparable' It doesn't help to use "where T1: struct, IComparable" neither. Michal

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #6

              Yes, don't use the greater-than or less-than operator to compare the values, since not all IComparable objects implement those operator methods (seems silly they don't, IMO). Instead, use this:

              if(value1.CompareTo(value2) > 0)
              {
              return value1;
              }
              return value2;

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Christian Zionism, as seen from a Jewish perspective The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              M 1 Reply Last reply
              0
              • J Judah Gabriel Himango

                Yes, don't use the greater-than or less-than operator to compare the values, since not all IComparable objects implement those operator methods (seems silly they don't, IMO). Instead, use this:

                if(value1.CompareTo(value2) > 0)
                {
                return value1;
                }
                return value2;

                Tech, life, family, faith: Give me a visit. I'm currently blogging about: Christian Zionism, as seen from a Jewish perspective The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                M Offline
                M Offline
                michal kreslik
                wrote on last edited by
                #7

                Thanks for the hint! The final compilable notation would be this: public static T1 GenericMethod(IComparable value1, IComparable value2) where T1 : struct { if (value1.CompareTo((T1)value2) > 0) { return (T1)value1; } return (T1)value2; } Michal

                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