是否有与回报(myVar的)回报myVar的有区别吗? [英] Is there a difference between return myVar vs. return (myVar)?

查看:237
本文介绍了是否有与回报(myVar的)回报myVar的有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一些例如C#code,并注意到一个例子包裹在()的返回的。

我总是刚刚做:

 收益myRV;

是有区别这样做的:

 收益率(myRV);


解决方案

更​​新:这个问题问得的我的博客12 2010年4月的主题。谢谢你的有趣的问题!

在实践中,没有差别。

的有可能是不同的。有在C#规范的地方,这可能present相差三个有趣的点。

首先,匿名函数转换为委托类型和前pression树木。考虑以下几点:

  Func键< INT> F1(){回报()=> 1; }
FUNC< INT> F2(){回报(()=大于1); }

F1 显然是合法的。为 F2 ?从技术上说,没有。 <一href=\"http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/csharp%20language%20specification.doc\">The规范说,在6.5节,有来自的拉姆达前pression 的到兼容的委托类型转换。那是一个的拉姆达前pression 的?不,这是一个的括号中的前pression 的包含的拉姆达前pression

在Visual C#编译器,使小规格违规这里并丢弃括号你。

二:

  INT M(){返回1; }
FUNC&LT; INT&GT; F3(){返回米; }
FUNC&LT; INT&GT; F4(){回报(M); }

F3 是合法的。为 F4 ?第7.5.3条规定,一个括号前pression可能不包含方法组。同样,为了您的方便,我们违反了规范,并允许进行转换。

第三:

 枚举,E {}无
ËF5(){返回0; }
ËF6(){回报(0); }

F5 是合法的。为 F6 ?号该规范规定,存在从字面零到任何枚举类型转换。 (0)不是字面零,这是一个括号后跟字面零,随后括号。在这里,我们违反了规范,实际上允许任何的编译时间常量前pression等于零的,并不仅仅是字面上为零。

因此​​,在任何情况下,我们让您摆脱它,即使在技术上这样做是违法的。

I was looking at some example C# code, and noticed that one example wrapped the return in ()'s.

I've always just done:

return myRV;

Is there a difference doing:

return (myRV);

解决方案

UPDATE: This question was the subject of my blog on 12 April 2010. Thanks for the amusing question!

In practice, there is no difference.

In theory there could be a difference. There are three interesting points in the C# specification where this could present a difference.

First, conversion of anonymous functions to delegate types and expression trees. Consider the following:

Func<int> F1() { return ()=>1; }
Func<int> F2() { return (()=>1); }

F1 is clearly legal. Is F2? Technically, no. The spec says in section 6.5 that there is a conversion from a lambda expression to a compatible delegate type. Is that a lambda expression? No. It's a parenthesized expression that contains a lambda expression.

The Visual C# compiler makes a small spec violation here and discards the parenthesis for you.

Second:

int M() { return 1; }
Func<int> F3() { return M; }
Func<int> F4() { return (M); }

F3 is legal. Is F4? No. Section 7.5.3 states that a parenthesized expression may not contain a method group. Again, for your convenience we violate the specification and allow the conversion.

Third:

enum E { None }
E F5() { return 0; }
E F6() { return (0); }

F5 is legal. Is F6? No. The spec states that there is a conversion from the literal zero to any enumerated type. "(0)" is not the literal zero, it is a parenthesis followed by the literal zero, followed by a parenthesis. We violate the specification here and actually allow any compile time constant expression equal to zero, and not just literal zero.

So in every case, we allow you to get away with it, even though technically doing so is illegal.

这篇关于是否有与回报(myVar的)回报myVar的有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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