Cook Computing

C# Methods Differing By Return Type Only

January 15, 2003 Written by Charles Cook

While doing some prototyping on how to handle optional struct members in XML-RPC.NET I realized that it is possible to define methods in C# which differ only by return type. This is a feature of the CLR which most languages don't support and C# is the same...except for the definition of operator conversion methods. For example:


class MyInteger
{
  ....
  public static implicit operator Int32(MyInteger mi) 
  {
    return mi.ToInt32;
  }
  public static implicit operator Single(MyInteger mi) 
  {
    return mi.ToSingle();
  }
  .... 
}

In the resulting IL these two methods have the following signatures:


public static  int32  op_Implicit(class XmlRpcInt mi)
public static  float32  op_Implicit(class XmlRpcInt mi)