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?
It depends on why you need this.
The most common approach is to just use a different name. The framework does this in many cases, such as the Convert class.
If you want to support multiple types, you can do this via generic methods:
The generic method approach is common for repostories, as it's common that there's an "Entity" type of base class.
You can have another parameter specifying the return type.
Typically you simply give the methods different names.
An example of this is
IDataReader
withGetInt32
,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:
I've already posted an answer, but here's another way: use
object
's.And cast the result to the appropriate type.