title: how to overload an operator of a generic class with operands of different generic types of the generic class
body: <p>There have been a lot of similar questions asked but all involve operands of same type or same generic type. This one in particular (<a href="https://stackoverflow.com/questions/15381285/how-can-i-use-a-generic-type-parameter-with-an-operator-overload">How can I use a generic type parameter with an operator overload?</a>) is close to what I am looking for but no answer or work-around.</p>
  
  <p>Is it possible to do something like this for the ‘*’ operator overload:</p>
  
  <pre><code>public class MyNumericClass&lt;T&gt;
  {
      public double Value { get; set; }
  
      //OK but works only for same type operands
      public static double operator *(MyNumericClass&lt;T&gt; left, MyNumericClass&lt;T&gt; right)
      {
          return left.Value * right.Value;
      }
  
      //****Pseudo Code for different type operands - not OK****
      public static double operator *&lt;TRight&gt;(MyNumericClass&lt;T&gt; left, MyNumericClass&lt;TRight&gt; right)
      {
          return left.Value * right.Value;
      }
  
      static void Test()
      {
          MyNumericClass&lt;int&gt; i = new MyNumericClass&lt;int&gt;();
          MyNumericClass&lt;int&gt; j = new MyNumericClass&lt;int&gt;();
          MyNumericClass&lt;string&gt; s = new MyNumericClass&lt;string&gt;();
          double r1 = i * j;
          double r2 = i * s; //Operator '*' cannot be applied to operands...
      }
  }
  </code></pre>
  
  <p>I need to have a specific overload for different generic types. A non-generic superclass will not do in this case as I have other overloads of the operator (not shown here) and ambiguous calls will be produced if I use a parameter of superclass type and not the exact generic type.</p>
  
  <p>Is it possible to do this or is there a work-around? Is it possible to use op_Multiply instead? (Tried it but couldn’t make it work).</p>
  
  <p>P.S. I don’t see any reason why something like this should not be possible.</p>
  
  <p><strong>EDIT1</strong>
  After people's answers and comments I am adding another version of the class with the implicit casts and more overloads to demonstrate the call ambiguity and why the answers provided do not solve my problem. I need to specify the different generic type in my operator overload to resolve this:</p>
  
  <pre><code>public class MyNumericClass&lt;T&gt;
  {
      public double Value { get; set; }
  
      public static implicit operator double(MyNumericClass&lt;T&gt; value)
      {
          return value.Value;
      }
  
      public static implicit operator MyNumericClass&lt;T&gt;(double value)
      {
          MyNumericClass&lt;T&gt; c = new MyNumericClass&lt;T&gt;();
          c.Value = value;
          return c;
      }
  
      public static MyNumericClass&lt;T&gt; operator *(double left, MyNumericClass&lt;T&gt; right)
      {
          return left * right.Value;
      }
  
      public static MyNumericClass&lt;T&gt; operator *(MyNumericClass&lt;T&gt; left, double right)
      {
          return right * left;
      }
  
      //Does not resolve ambiguity and neither does a base class or interface
      public static double operator *(MyNumericClass&lt;T&gt; left, dynamic right)
      {
          return right * left;
      }
  
      //OK but work only for same type operands
      public static double operator *(MyNumericClass&lt;T&gt; left, MyNumericClass&lt;T&gt; right)
      {
          return left.Value * right.Value;
      }
  
      ////****Pseudo Code for different type operands - not OK****
      //public static double operator *&lt;TRight&gt;(MyNumericClass&lt;T&gt; left, MyNumericClass&lt;TRight&gt; right)
      //{
      //    return left.Value * right.Value;
      //}
  
      static void Test()
      {
          MyNumericClass&lt;int&gt; i = new MyNumericClass&lt;int&gt;();
          MyNumericClass&lt;int&gt; j = new MyNumericClass&lt;int&gt;();
          MyNumericClass&lt;string&gt; s = new MyNumericClass&lt;string&gt;();
          double r1 = i * j;
          double r2 = i * s; //The call is ambiguous...
      }
  }
  </code></pre>