Connecting Tech Pros Worldwide Help | Site Map

Using math operator with generics

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 17th, 2005, 10:16 AM
Marco Segurini
Guest
 
Posts: n/a
Default Using math operator with generics

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.

  #2  
Old November 17th, 2005, 10:16 AM
Christoph Nahr
Guest
 
Posts: n/a
Default 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
  #3  
Old November 17th, 2005, 10:17 AM
Marcus Andrén
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.