为什么不能重载考虑返回类型。 [英] Why can't overloads take into account the return type.

查看:63
本文介绍了为什么不能重载考虑返回类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如


void DoIt()

{

int i = FromString(" 1");

double d = FromString(" 1.1");

}


int FromString(string SomeValue)

{

返回int.Parse(SomeValue);

}


double FromString(string SomeValue)

{

返回double.Parse(SomeValue);

}


返回类型实际上只是一个无论如何我在下面的byref param。

解决方案

" Michael C" < MC ***** @ NOSPAMoptushome.com.au>写在

新闻:#j ************** @ TK2MSFTNGP09.phx.gbl:

例如

void DoIt()
{i /> int i = FromString(" 1");
double d = FromString(" 1.1");
} int /> int FromString(string SomeValue)
返回int.Parse(SomeValue);
}

双FromString(string SomeValue )
{
返回double.Parse(SomeValue);
}

返回类型实际上只是我下面的一个byref param
推测。 / blockquote>


因为函数调用和赋值结果是两个完全不同的东西。你甚至不必分配结果。所以,告诉

me ...例如,如果我这样做会发生什么:


//这不会被分配到任何地方 - 哪个我应该拨打一个电话吗?

FromString(" 3.14159");


//这应该返回什么 - 一个double还是一个int?

int i =(int)FromString(" 3.14159");


//甚至更糟......

d d =(int)FromString(" 3.14159");


我相信如果它确实考虑了这将是多么荒谬可以获得

返回类型...


-mdb


" mdb" < m_b_r_a_y @ c_t_i_u_s_a__d0t__com>在消息中写道

news:Xn **************************** @ 207.46.248.16。 ..

因为函数调用和赋值结果是两个完全不同的东西。


在VB6中,返回值只是引擎盖下的另一个参数。

实际返回值是错误代码,我认为它在C#中是相同的。

如果我错了,有人会纠正我。

你甚至不必分配结果。所以,告诉我......例如,如果我这样做会发生什么:


在任何一种情况下,它都会产生一个错误,就像完全一样如果它无法解决超载问题,那么它将是b $ b。请参阅下面的代码。

我相信如果它确实考虑了
返回类型,你会发现这会有多荒谬......




完全没有。在这种情况下,开发人员通过创建2个重载选择具有返回类型

部分签名,唯一的区别是返回类型

。因为它可以打开和关闭我看不出任何问题

用它。


Michael


[STAThread]

static void Main()

{

DoIt(null);

}


静态无效DoIt(Class1 SomeValue)

{

}


静态无效DoIt(Class2 SomeValue)

{

}




" Michael C" ; < MC ***** @ NOSPAMoptushome.com.au>在消息中写道

news:ex ************** @ TK2MSFTNGP14.phx.gbl ...

" mdb" < m_b_r_a_y @ c_t_i_u_s_a__d0t__com>在消息中写道
新闻:Xn **************************** @ 207.46.248.16。 ..

因为函数调用和分配结果是两个完全不同的东西。



在VB6中,返回值只是另一个参数引擎盖。
实际返回值是错误代码,我认为它在C#中是相同的。
如果我错了,有人会纠正我。




抱歉,你错了C#。


eg

void DoIt()
{
int i = FromString("1");
double d = FromString("1.1");
}

int FromString(string SomeValue)
{
return int.Parse(SomeValue);
}

double FromString(string SomeValue)
{
return double.Parse(SomeValue);
}

The return type is really just a byref param underneath anyway I presume.

解决方案

"Michael C" <mc*****@NOSPAMoptushome.com.au> wrote in
news:#j**************@TK2MSFTNGP09.phx.gbl:

eg

void DoIt()
{
int i = FromString("1");
double d = FromString("1.1");
}

int FromString(string SomeValue)
{
return int.Parse(SomeValue);
}

double FromString(string SomeValue)
{
return double.Parse(SomeValue);
}

The return type is really just a byref param underneath anyway I
presume.



Because the function call and assigning the result are two completely
different things. You don''t even have to assign the result. So, tell
me... For example, what would happen if I did these:

// This doesn''t get assigned anywhere - which one should I call?
FromString("3.14159");

// What should this return - a double or an int?
int i = (int)FromString("3.14159");

// Or even worse...
double d = (int)FromString("3.14159");

I''m sure you can see how ridiculous this would get if it did consider
return type...

-mdb


"mdb" <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote in message
news:Xn****************************@207.46.248.16. ..

Because the function call and assigning the result are two completely
different things.
In VB6 the return value was just another parameter under the hood. The
actual return value was the error code, which I presume is the same in C#.
Someone correct me if i''m wrong.
You don''t even have to assign the result. So, tell
me... For example, what would happen if I did these:
In any of those cases it would give an error in exactly the same way as it
would if it couldn''t resolve an overload. See code below.
I''m sure you can see how ridiculous this would get if it did consider
return type...



Not at all. In this case the developer has chosen to have the return type
part of the signature by creating 2 overloads with the only difference being
the return type. Because it can be turned on and off I can''t see any problem
with it.

Michael

[STAThread]
static void Main()
{
DoIt(null);
}

static void DoIt(Class1 SomeValue)
{
}

static void DoIt(Class2 SomeValue)
{
}



"Michael C" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...

"mdb" <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote in message
news:Xn****************************@207.46.248.16. ..

Because the function call and assigning the result are two completely
different things.



In VB6 the return value was just another parameter under the hood. The
actual return value was the error code, which I presume is the same in C#.
Someone correct me if i''m wrong.



Sorry, you''re wrong about C#.


这篇关于为什么不能重载考虑返回类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆