By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,567 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,567 IT Pros & Developers. It's quick & easy.

How to handle int and float in one class

P: n/a
Lee
I'd like to implement a generic class that takes as its Type-parameter
a numeric type (e.g. (U)Int16/32/64, float, double, decimal, etc.).

The problem is that, having validated the Type-parameter, I cannot do
any arithmetic operations (+, -, *, /) on variables of that type.

I tried creating wrapper-classes for the numeric types I will support
and tried to derive them from a custom interface that specified the
above 4 operators -- but interfaces are not allowed to specify
operators.

The only solution I can think of is to use a 'switch' statement at the
places I need to do the arithmetic operations -- but to me that is a
very poor solution.

I'm fairly sure this problem has arisen before and I'm hoping someone
can point me to a viable solution.

--
Lee Silver
Information Concepts Inc.
Jul 4 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Lee wrote:
I'd like to implement a generic class that takes as its Type-parameter
a numeric type (e.g. (U)Int16/32/64, float, double, decimal, etc.).

The problem is that, having validated the Type-parameter, I cannot do
any arithmetic operations (+, -, *, /) on variables of that type.

I tried creating wrapper-classes for the numeric types I will support
and tried to derive them from a custom interface that specified the
above 4 operators -- but interfaces are not allowed to specify
operators.
I'm fairly sure this problem has arisen before and I'm hoping someone
can point me to a viable solution.
You need to use an abstract base class or (preferably) an interface that
defines the operations you want as methods. You can't do it just with
operators.

The general pattern is the same as with IComparer<Tand
IEqualityComparer<T- these provide equivalents of '<' and '=='
respectively, in the form of Compare and Equals, respectively.

You then pass an implementation of your IArithmetic<Tto your generic
class's constructor, or methods that require such operations. If you do
it right, you can even avoid virtual / interface method calls:

---8<---
using System;

interface IArithmetic<T>
{
T Add(T l, T r);
}

struct Int32Arithmetic: IArithmetic<int>
{
public int Add(int l, int r) { return l + r; }
}

class App
{
static T Sum<T,TOps>(T[] array, TOps ops)
where TOps: IArithmetic<T>
{
T result = default(T);
foreach (T item in array)
result = ops.Add(result, item);
return result;
}

static void Main()
{
int[] values = { 10, 20, 30 };
Console.WriteLine(
Sum<int,Int32Arithmetic>(values,
new Int32Arithmetic()));
}
}
--->8---

The calls to ops.Add above are resolved statically due to (1) TOps being
constrained to an interface, (2) the implementation type specified
directly, and (3) the concrete implementation of the interface being on
a sealed type, such as a struct.

Obviously, if you're doing lots of this, you'll want to specify TOps &
ops in a descendant class & constructor, to avoid verbosity.

With appropriate reflection tricks, an equivalent to Comparer<T>.Default
(that might be called Arithmetic<T>.Default, for example) can be
created.

-- Barry

--
http://barrkel.blogspot.com/
Jul 4 '08 #2

P: n/a
On Jul 4, 5:15*am, Lee <lsil...@information-concepts.comwrote:
I'd like to implement a generic class that takes as its Type-parameter
a numeric type (e.g. (U)Int16/32/64, float, double, decimal, etc.).

The problem is that, having validated the Type-parameter, I cannot do
any arithmetic operations (+, -, *, /) on variables of that type.
You'd probably find the Operator class in MiscUtil useful. MiscUtil is
a free library I've got: http://pobox.com/~skeet/csharp/miscutil

The Operator class is written by Marc Gravell.

See http://pobox.com/~skeet/csharp/miscu...operators.html
for more details.

Jon
Jul 4 '08 #3

P: n/a
Lee
On Jul 4, 1:56 pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jul 4, 5:15 am, Lee <lsil...@information-concepts.comwrote:
I'd like to implement a generic class that takes as its Type-parameter
a numeric type (e.g. (U)Int16/32/64, float, double, decimal, etc.).
The problem is that, having validated the Type-parameter, I cannot do
any arithmetic operations (+, -, *, /) on variables of that type.

You'd probably find the Operator class in MiscUtil useful. MiscUtil is
a free library I've got:http://pobox.com/~skeet/csharp/miscutil

The Operator class is written by Marc Gravell.

Seehttp://pobox.com/~skeet/csharp/miscutil/usage/genericoperators.html
for more details.

Jon
Jon:

Thanks for the response.

I found the links you mentioned after posting my message (my bad for
not doing a more thorough search ahead of time :) -- I was about to
post that I found the ones you cited when I saw your response.)

Anyway, they are exactly what I am looking for.

Much thanks.

--
Lee Silver
Information Concepts Inc.
Jul 4 '08 #4

P: n/a
If you get any problems, let me know; I also have a 2.0 version
somewhere, but it isn't as complete nor unit tested.

Marc
Jul 4 '08 #5

P: n/a
On Jul 4, 8:04*am, Marc Gravell <marc.grav...@gmail.comwrote:
If you get any problems, let me know; I also have a 2.0 version
somewhere, but it isn't as complete nor unit tested.
We ought to do something about this - and the fact that it's currently
in MiscUtil, despite being a clearly desirable entity in its own
right. I think it may be time to start thinking about a
code.google.com project just for the operator stuff. The reason I'm
posting this instead of mailing it straight to Marc is that it would
be good to get more people involved. Quite how we then migrate it
*out* of MiscUtil is a different question, but we can tackle that
later.

Jon
Jul 4 '08 #6

P: n/a
No problem here; my only thought is that there isn't really much left to
do with it now (I can't envisage any huge changes / feature requests) so
it would mainly be a code dump, not an active project.

I like the employer-loyalty, though; last time we discussed this
(probably re the whole of MiscUtil), I'm sure you were talking about
another code site ;-p

Marc
Jul 4 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.