C# Template for int and float -


i have 2 classes, 1 used float , 1 used int. code same , write template class compatible both int , float in order not copy code different type.

here class :

namespace xxx.schema {     public abstract class numericpropdef< numerictype > : propdef         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 below minimum" );             maximum = newmax;         }          public void setdefault( numerictype? def )         {             default = def;         }     } } 

but reasons don't know, i'm getting following error :

error cs0019: operator '>' cannot applied operands of type 'numerictype' , 'numerictype' 

i'm used c++ templates, not c# templates i'm bit lost here. reason of ? thank you.

without specifying else, generic parameter (such numerictype) assumed have same capabilities system.object. why? well, because users of class might pass system.object numerictype parameter. so, not guaranteed type passed generic parameter supports > operator, , hence compiler doesn't allow use it.

now, have restricted numerictype, in require type passed numerictype implement icomparable<t> , structure. however, neither of these restrictions guarantees there > operator, still cannot use it.

in particular case, might want use compareto method, availability on type passed numerictype is guaranteed requirement type implement icomparable<t>. note, however, this, class can used loads of other types have nothing numbers, if poses problem you.

in general, particular quest finding restriction lets users supply numeric type cannot answered in c#, numeric types in c# (or cli in general) not inherit common base class numeric types.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -