Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Using math operator with generics

Question posted by: Marco Segurini (Guest) on November 17th, 2005 10:16 AM
Hi,

The following code shows my problem:

using System;
using System.Collections.Generic;
using System.Text;

....

namespace ConsoleApplication1
{
static class MyOp
{
public static T Sub<T>(T item1, T item2)
where T : struct
{
return item1-item2; // this is line 118
}
}

class Program
{
static void Main(string[] args)
{
Console.WriteLine(MyOp.Sub(2,3));
}
}
}

Build result:
Program.cs(118,17): error CS0019: Operator '-' cannot be applied to
operands of type 'T' and 'T'

What have I to add to 'where' to tell to the generic function that T
implements operator- ?

Another question: the C# built-in types (byte,int,double,...) have
associated the same set of operation {+,-,*,/}: are these types derived
from common interface?

TIA.
Marco.
Christoph Nahr's Avatar
Christoph Nahr
Guest
n/a Posts
November 17th, 2005
10:16 AM
#2

Re: Using math operator with generics
On Mon, 17 Oct 2005 10:55:57 +0200, Marco Segurini
<marcosegurini@virgilio.it> wrote:
[color=blue]
>What have I to add to 'where' to tell to the generic function that T
>implements operator- ?[/color]

You can't do that. There is no operator constraint in C# 2.0.
[color=blue]
>Another question: the C# built-in types (byte,int,double,...) have
>associated the same set of operation {+,-,*,/}: are these types derived
>from common interface?[/color]

They are not. There have been a few threads on this subject before,
and the conclusion was that it is impossible to perform any of the
built-in mathematical operations on generic types.

If you want to write a class that works on different numeric types you
have to it the old-fashioned way -- provide strongly-typed overloads
for each version. Generics are no help here, unfortunately.
--
http://www.kynosarges.de

Marcus Andrén's Avatar
Marcus Andrén
Guest
n/a Posts
November 17th, 2005
10:17 AM
#3

Re: Using math operator with generics
On Mon, 17 Oct 2005 10:55:57 +0200, Marco Segurini
<marcosegurini@virgilio.it> wrote:[color=blue]
>
>What have I to add to 'where' to tell to the generic function that T
>implements operator- ?
>
>Another question: the C# built-in types (byte,int,double,...) have
>associated the same set of operation {+,-,*,/}: are these types derived
>from common interface?
>[/color]

Unfortunally operators can't belong to interfaces so there is no
direct way of doing it. If you still want/need it, there is an
excellent article that describes how to work around the problem. It
can be found at

http://www.codeproject.com/csharp/genericnumerics.asp

--
Marcus Andrén

 
Not the answer you were looking for? Post your question . . .
189,282 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors