c#return语句 [英] the c# return statement

查看:91
本文介绍了c#return语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



c#* return *语句在过去的几个月一直困扰着我。


我不喜欢你可以拥有的事实方法中的不同代码路径

并且有多个return语句。对我来说,如果一个方法只能有一个返回语句,它会更正交





-

W''04<:>开源

解决方案

John Bailo写道:

c#* return *语句有过去几个月一直在困扰我。

我不喜欢你可以在一个方法中拥有不同的代码路径并且有多个return语句。对我来说,如果一个方法只能有一个return语句,那就更正交了。




在某种程度上,我分享这个问题:-)我发现

德尔福的'/ Pascal'的''结果''返回机制更优雅,对于

大部分。但是,我认为有/某些/实例在方法结束之前返回

可以产生更直接的代码。


Delphi示例' 's / Pascal'的返回机制:


函数Foo:布尔值;

开始

结果:=真;

如果FooBar那么

结果:= GiveMeABoolean();

DoSomethingElse();

end; //返回值是Result的值


当然,这可以很容易地在C#中模拟(但它通常涉及更多

语句):


bool Foo()

{

bool result = true;


if( fooBar)

result = GiveMeABoolean();

DoSomethingElse();


返回结果;

}


甚至更好,在这种情况下:


bool Foo()

{

bool result = fooBar? GiveMeABoolean():true;

DoSomethingElse();

返回结果;

}


我想我不介意C风格的返回机制,如果它不被滥用(在我看来,根据我自己的个人风格),但是找到

Delphi'/ Pascal'在大多数情况下相当优雅。


John Bailo写道:

过去几个月,c#* return *语句一直困扰着我。

我不喜欢你可以在一个方法中拥有不同的代码路径并且有多个返回的事实声明。对我来说,如果一个方法只能有一个return语句,那就更正交了。




在某种程度上,我分享这个问题:-)我发现

德尔福的'/ Pascal'的''结果''返回机制更优雅,对于

大部分。但是,我认为有/某些/实例在方法结束之前返回

可以产生更直接的代码。


Delphi示例' 's / Pascal'的返回机制:


函数Foo:布尔值;

开始

结果:=真;

如果FooBar那么

结果:= GiveMeABoolean();

DoSomethingElse();

end; //返回值是Result的值


当然,这可以很容易地在C#中模拟(但它通常涉及更多

语句):


bool Foo()

{

bool result = true;


if( fooBar)

result = GiveMeABoolean();

DoSomethingElse();


返回结果;

}


甚至更好,在这种情况下:


bool Foo()

{

bool result = fooBar? GiveMeABoolean():true;

DoSomethingElse();

返回结果;

}


我想我不介意C风格的返回机制,如果它不被滥用(在我看来,根据我自己的个人风格),但是找到

Delphi'/ Pascal'在大多数情况下相当优雅。


John Bailo写道:


c#* return *语句在过去的几个月一直困扰着我。

我不喜欢你可以在一个方法中使用不同的代码路径
并有多个return语句。对我来说,如果一个方法只能有一个return语句,那就更正交了。




C#语言非常基于C ++和Java,多次返回

语句是该语言的一部分。


如果您遇到多个返回语句的问题,请不要使用它们。



The c# *return* statement has been bothering me the past few months.

I don''t like the fact that you can have different code paths in a method
and have multiple return statements. To me, it would be more orthogonal
if a method could only have one return statement.


--
W ''04 <:> Open Source

解决方案

John Bailo wrote:

The c# *return* statement has been bothering me the past few months.

I don''t like the fact that you can have different code paths in a method
and have multiple return statements. To me, it would be more orthogonal
if a method could only have one return statement.



To some extent, I share this sentement :-) I find that
Delphi''s/Pascal''s ''result'' returning mechanism is more elegant, for the
most part. However, I think there are /some/ instances where returning
before the end of a method can produce more straight-forward code.

Example of Delphi''s/Pascal''s returning mechanism:

function Foo: Boolean;
begin
Result := True;
if FooBar then
Result := GiveMeABoolean();
DoSomethingElse();
end; // return value is value of Result

Of course, this can easily be simulated in C# (but it involves more
statements, generally):

bool Foo()
{
bool result = true;

if (fooBar)
result = GiveMeABoolean();
DoSomethingElse();

return result;
}

Or even better, in this case:

bool Foo()
{
bool result = fooBar ? GiveMeABoolean() : true;
DoSomethingElse();
return result;
}

I guess I don''t mind the C-style returning mechanism so much if it''s not
abused (in my opinion, according to my own personal style), but find
Delphi''s/Pascal''s equivalent more elegant in most cases.


John Bailo wrote:

The c# *return* statement has been bothering me the past few months.

I don''t like the fact that you can have different code paths in a method
and have multiple return statements. To me, it would be more orthogonal
if a method could only have one return statement.



To some extent, I share this sentement :-) I find that
Delphi''s/Pascal''s ''result'' returning mechanism is more elegant, for the
most part. However, I think there are /some/ instances where returning
before the end of a method can produce more straight-forward code.

Example of Delphi''s/Pascal''s returning mechanism:

function Foo: Boolean;
begin
Result := True;
if FooBar then
Result := GiveMeABoolean();
DoSomethingElse();
end; // return value is value of Result

Of course, this can easily be simulated in C# (but it involves more
statements, generally):

bool Foo()
{
bool result = true;

if (fooBar)
result = GiveMeABoolean();
DoSomethingElse();

return result;
}

Or even better, in this case:

bool Foo()
{
bool result = fooBar ? GiveMeABoolean() : true;
DoSomethingElse();
return result;
}

I guess I don''t mind the C-style returning mechanism so much if it''s not
abused (in my opinion, according to my own personal style), but find
Delphi''s/Pascal''s equivalent more elegant in most cases.


John Bailo wrote:


The c# *return* statement has been bothering me the past few months.

I don''t like the fact that you can have different code paths in a method
and have multiple return statements. To me, it would be more orthogonal
if a method could only have one return statement.



The C# language is very much based on C++ and Java, multiple return
statements are part of the language.

If you have a problem with multiple return statements, don''t use them.


这篇关于c#return语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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