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