为什么它需要作为返回类型? [英] void why is it needed as a return type?

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

问题描述

好奇的是,我们不再需要在没有

参数的方法上使用void;


void methodWithNoParameters(void)已被void

methodWithNoParameters()。


我们还有一个很好的理由我们仍然需要为一个方法指定void吗

不返回任何结果?例如;


private void methodWithNoReturn()可以被私有替换

methodWithNoReturn()。

解决方案




它是C#语言的定义,也使得事物明确改进

代码理解。


祝你好运

Salva

" ge *************** **@yahoo.co.uk"写道:

只是好奇,我们不再需要一个不带
参数的方法的void;

void methodWithNoParameters(void)已被替换by void
methodWithNoParameters()。

我们还有一个很好的理由为
不返回结果的方法指定void吗?例如;

private void methodWithNoReturn()可以被私有
methodWithNoReturn()取代。



< blockquote>你如何区分以下


类Foo

{

//这是默认构造函数

public Foo()

{

}

//这是一个名为Foo的方法,什么都不返回

public Foo()

{

}

}


问候


Richard Blewett - DevelopMentor
http:/ /www.dotnetconsult.co.uk/weblog
http:// www.dotnetconsult.co.uk


只是好奇,我们不再需要在没有

参数的方法上无效;


void methodWithNoParameters(void)已被void取代

methodWithNoP arameters()。


我们还有一个很好的理由,我们仍然需要为一个方法指定void,

不会返回结果吗?例如;


private void methodWithNoReturn()可以被私有替换

methodWithNoReturn()。


萨尔瓦多,

仅仅因为它的语言定义并不意味着它是最好的

做事方式。你也明确没有在方法原型上有一个返回类型

,所以你明确地说没有

返回类型。
< br $> b $ b理查德,

好​​点。在做事方面你不会允许名为

的方法与它们被调用的类相同 - 它会抛出编译器

错误。你有什么理由可以说为什么一个方法必须和班级一样被称为



这两种方式都没什么大不了的......


Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().

解决方案

Hi,

It is the C# lenguage definition, and also making things explicit improves
the code understanding.

Best regards
Salva
"ge*****************@yahoo.co.uk" wrote:

Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().



How would you differentiate the following

class Foo
{
// this is a default constructor
public Foo()
{
}
// this is a method called Foo that returns nothing
public Foo()
{
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().


Salvador,
Just because it''s the language definition doesn''t mean it''s the best
way of doing things. Also you are explicitly not having a return type
on the method prototype and so you''re explicitly saying there''s no
return type.

Richard,
Good point. In way of doing things you''d not allow methods named the
same as the class they are being called on - it''ll throw a compiler
error. Can you thing of a good reason why a method would have to be
called the same as the class?

Either way it''s no big deal...


这篇关于为什么它需要作为返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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