C# Overload return type - recommended approach

richard picture richard · May 23, 2013 · Viewed 10.4k times · Source

I have a situation where I just want the return type to be different for a method overload, but you can't do this in C#.

What's the best way to handle this? Is the fact that I need this mean my program structure flawed?

Given the fact that this is impossible in C#, what is the recommended approach?

Answer

Jon Skeet picture Jon Skeet · May 23, 2013

Typically you simply give the methods different names.

An example of this is IDataReader with GetInt32, GetInt64 etc.

To be honest, I believe this usually makes it clearer what you're trying to do anyway - particularly if you're then calling an overloaded method with the result:

Console.WriteLine(reader.GetInt32()); // This is clear

Console.WriteLine(foo.OverloadedGet()); // Which overload of OverloadedGet?