Question regarding generics and type constraints



I'm new to Generics (years and years of VS.NET 2003 development, but very
little .NET 2.0+). I have some routines for conversion from RGB to different
color spaces and back. I would like the routines to support, at least, byte,
int, and double types without having to create different versions of the
routines. Is there any way to do this with generics?

The problem is, if I do something like this:

public static void RGB_to_YIQ<T>(T[] R, T[] G, T[] B, T[] Y, T[] I, T[] Q)
{
Y[index] = 0.299 * R[index] + 0.587 * G[index] + 0.114 * B[index];
...
}

I get an error: "Operator '*' cannot be applied to operands of type 'double'
and 'T'"

Like I said, I'm a newb to the whole generics thing. I read the "Constraints
on Type Parameters" stuff using "where" in MSDN, but I'm not sure how I can
use that, if I can.

Thanks.



.



Relevant Pages

  • Re: optional arguments in subroutine or using generic procedure
    ... subroutine with optional arguments declared. ... How logically related are the two routines? ... two separate routines. ... I think generics like the intrinsic ones also help clarity by ...
    (comp.lang.fortran)
  • Re: optional arguments in subroutine or using generic procedure
    ... different names and using generic procedure, ... subroutine with optional arguments declared. ... How logically related are the two routines? ... I think generics like the intrinsic ones also help clarity by ...
    (comp.lang.fortran)
  • Re: Question regarding generics and type constraints
    ... I have some routines for conversion from RGB to ... I'm a newb to the whole generics thing. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing information from Unmanaged to Managed code
    ... When I tried using Generics in the interface declaration, ... > declaration, you will have to wrap the whole object, and expose the methods ... >>> generic instance, setting the type parameters, and expose that to COM. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Anonymous Method/Properties etc with
    ... The big reason is because of the way that generics are handled at run ... The CLR is actually aware of them as generics. ... > type parameters by default because the compiler will complain about ...
    (microsoft.public.dotnet.languages.csharp)