Really impossible to use return type overloading?

ECMA-334 C# Section 8.7.3

The signature of a method consists of
the name of the method and the number,
modifiers, and types of its formal
parameters. The signature of a method
does not include the return type.

You could use a generic method:

T AddNumbers<T>(int a, int b)
{
   if (typeof(T) == typeof(int) || typeof(T) == typeof(float))
   {
      return (T)Convert.ChangeType(a + b, typeof(T));
   }

   throw new NotSupportedException();
}

Leave a Comment