I have two classes, one used for float and one used for int. Their code is exactly the same and I would like to write a template class that would be compatible with both int and float in order not to copy this code with just a different type.
Here is my class :
namespace XXX.Schema
{
public abstract class NumericPropDef< NumericType > : PropDef
where NumericType : struct, IComparable< NumericType >
{
public NumericType? Minimum { get; protected set; }
public NumericType? Maximum { get; protected set; }
public NumericType? Default { get; protected set; }
public NumericPropDef() : base() { }
public void SetMinimum( NumericType? newMin )
{
if( null != newMin && null != Maximum && (NumericType) newMin > (NumericType) Maximum )
throw new Exception( "Minimum exceeds maximum" );
Minimum = newMin;
}
public void SetMaximum( NumericType? newMax )
{
if( null != newMax && null != Minimum && (NumericType) newMax < (NumericType) Minimum )
throw new Exception( "Maximum is below minimum" );
Maximum = newMax;
}
public void SetDefault( NumericType? def )
{
Default = def;
}
}
}
But for reasons I don't know, I'm getting the following error :
error CS0019: Operator '>' cannot be applied to operands of type 'NumericType' and 'NumericType'
I'm used to C++ templates, but not to C# templates so I'm a bit lost here. What could be the reason of that ? Thank you.