Overriding operators in generic c# classes

Generics are cool. Unlike C++, it is easy to build a generic class and create a DLL library directly from it. The only real drawback is that it is difficult (perhaps impossible) to override operators.

There are a number of approaches to overriding arithmetic and logical operators in a generic C# class. Unfortunately, those that I have found so far are either too cumbersome and circuitous or are incomprehensible (at least to me). See, for example:
Operator Overloading with generics - using inheritance to allow use of C# operators (C# 2005)
http://www.codeproject.com/useritems/GenericCodeAndOperators.asp
An article on how to use C# operators on the parameter types in your generic code (normally the compiler does not allow this). This solution just uses inheritance. But the end-user still cannot do something like:

class Vector<T>
{
//...
}

class Program
{

static void Main(string[] args)
{
Vector<double> v1, v2 v3;
v1 = new Vector<double>(3);
v2 = new Vector<double>(3);
v3 = new Vector<double>();
v1[0] = 1.1; v1[1] = 1.2; v1[2] = 1.3;
v2[0] = 2.1; v2[2] = 2.2; v2[3] = 2.3;

v3 = v1 + v2; // compiler error
v3 = v1 - v2; // compiler error
v3 = v1 * v2; // compiler error

}

Any ideas on how best to solve this problem. After all, if you can't override operators, what good are generic classes anyway ?

Mike :wave:
[1510 byte] By [Mike Pliam] at [2007-11-20 11:43:43]
# 1 Re: Overriding operators in generic c# classes
The reason for not allowing operator overloading is that Vector2<Socket> can't possibly be 'added' to another Vector2<Socket>. I don't believe there's any nice way around the problem. I know i was talking to a guy before who was doing a game engine in c# and he settled on the approach of just using seperate structs for the different basic geometry classes like Vector because 1) He could override operators, 2) It is much much faster.
Mutant_Fruit at 2007-11-9 11:37:02 >
# 2 Re: Overriding operators in generic c# classes
It is much much faster.

Do we know this to be true? Has anyone compared several generic vs non-generic methods as to timing?

If this is indeed the case, I think I will stop wasting my time trying to build code that is significantly less efficient.

Mike :)
Mike Pliam at 2007-11-9 11:38:03 >
# 3 Re: Overriding operators in generic c# classes
It is much much faster.

Do we know this to be true? Has anyone compared several generic vs non-generic methods as to timing?

If this is indeed the case, I think I will stop wasting my time trying to build code that is significantly less efficient.

Mike :)
Mike Pliam at 2007-11-9 11:39:02 >
# 4 Re: Overriding operators in generic c# classes
Do we know this to be true? Has anyone compared several generic vs non-generic methods as to timing?

If this is indeed the case, I think I will stop wasting my time trying to build code that is significantly less efficient.

Mike :)
Unfortunately i don't have his benchmarks and his numbers anymore. You could always run a few micro-benchmarks yourself to check how long the different operations take.
Mutant_Fruit at 2007-11-9 11:40:07 >