generic problem with operator. compile error: operator * is not allowedon type T.

  • Thread starter Thread starter SharpKnight
  • Start date Start date
S

SharpKnight

Hi,

I am trying to use Generics to create a general method which accepts a
number as a parameter (integer, decimal, ...) and then performs some
calculations.

Below is a simplified example:

private static T test(T somenumber)
{
return ( somenumber * 2);
}

The problem is that this does not compile because the operator * is not
allowed on type T.

How can I solve this without using reference types/objects to do the
calculation ?
I tried adding the :

where T: struct

but it did not help.

I know c# 2.0 operators can not be defined in an interface so I also am
not able to define a different WHERE clause accepting the operator *.

Is there anybody that can help me with this ?

Thanks VERY MUCH in advance,

Regards,
Serge
 
Thanks very much! it helped me solve this puzzle



Lebesgue schreef:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top