C#中超载的返回类型 - 推荐的方法 [英] C# Overload return type - recommended approach
问题描述
我有一个情况我只想返回类型是一个方法重载不同,但你不能在C#中做到这一点。
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?
鉴于这是在C#中是不可能的,有什么建议的方法?
Given the fact that this is impossible in C#, what is the recommended approach?
推荐答案
通常情况下,你只需给方法不同的名字。
Typically you simply give the methods different names.
这方面的一个例子是的IDataReader
与 GetInt32等
, GetInt64
等。
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?
这篇关于C#中超载的返回类型 - 推荐的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!