Re: generics - constraint



Michael Moreno wrote:
I am trying to write a generic class where the underlying type is
either an int, a float or a double:

public class CMatrix<T>: ICloneable where VarType: int, float, double

but that does not compile.

The error message is :

'int' is not a valid constraint. A type used as a constraint must be an
interface, a non-sealed class or a type parameter.

Is it possible to do what I want?

No, I think the best you can do is write a static constructor that
checks typeof(T) and throws an exception if it isn't int, float, or
double.

Jesse

.



Relevant Pages

  • Re: Value / primitive type constraints on generics
    ... > What I think he meant to ask was will the constraint using "struct" ... > allow an int to be passed for the type parameter T. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Value / primitive type constraints on generics
    ... What I think he meant to ask was will the constraint using "struct" ... allow an int to be passed for the type parameter T. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: memory management in Ada: tedious without GC?
    ... float v; ... I'm talking about all objects whose constraint are not known at compile time. ...
    (comp.lang.ada)
  • Re: Abnormal program termination
    ... foo.c:4: warning: function declaration isn't a prototype ... Your version is legal under C90 rules, but int mainis more explicit ... float ave(), average; ...
    (comp.lang.c)
  • RTF Render with Scale
    ... float fHorzSizeInches = nHorzSize / 25.4f; ... IntPtr hdc = graphics.GetHdc; ... int nHorzSize = SafeNativeMethods.GetDeviceCaps(hdc, ... PHYSICALWIDTH = 110, ...
    (microsoft.public.dotnet.languages.csharp)