Overriding operators in generic c# classes
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:

