首页 新闻 会员 周边

为什么C#和VB.net都不支持基于返回值类型的重载?

0
[已解决问题] 解决于 2011-10-24 17:05

为什么C#和VB.net都不支持基于返回值类型的重载

4life的主页 4life | 菜鸟二级 | 园豆:210
提问于:2011-10-21 11:49
< >
分享
最佳答案
1

Eric Lippert大大对于"为什么C#/.NET不……"一类的问题是这么回答的:

As I'm fond of pointing out, the answer to every question of the form "why doesn't product X have feature Y?" is the same. It's because in order for a product to have a feature, that feature must be:
- thought of in the first place
- desired
- designed
- specified
- implemented
- tested
- documented
- shipped to customers
You've got to hit every single one of those things, otherwise, no feature.

“支持基于返回值类型的重载”是合理的(Java就可以,如果我没记错的话),但是C#不是Java,它“被设计成了这样”。答案就是这样。另外,如果你真的很需要返回值类型的重载,你可以选择其他的途径去解决,比如泛型或者out关键字一类的。总归是有替代解决方案的。

水牛刀刀 | 大侠五级 |园豆:6350 | 2011-10-21 15:30

说得好。

4life | 园豆:210 (菜鸟二级) | 2011-10-21 15:42
其他回答(2)
0

因为返回类型是在返回时才能确定的。

如 int f();与 string f();

系统无法知道你是调用哪个函数,要等到计算结果出来,才知道

小彬 | 园豆:947 (小虾三级) | 2011-10-21 12:41
0

如 int f();与 string f();

调用时候写 f();请问是调用哪个呢?返回值我可以丢掉不用,哈哈~~

stevey | 园豆:335 (菜鸟二级) | 2011-10-24 17:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册