我正在查看一些示例C#代码,并注意到一个示例将返回包含在()中.
我一直都这么做:
return myRV;
Run Code Online (Sandbox Code Playgroud)
这样做是否有区别:
return (myRV);
Run Code Online (Sandbox Code Playgroud)
Eri*_*ert 229
更新:这个问题是我的博客2010年4月12日的主题.谢谢你这个有趣的问题!
在实践中,没有区别.
在理论上有可能是一个区别.C#规范中有三个有趣的点,这可能会产生差异.
首先,将匿名函数转换为委托类型和表达式树.考虑以下:
Func<int> F1() { return ()=>1; }
Func<int> F2() { return (()=>1); }
Run Code Online (Sandbox Code Playgroud)
F1显然是合法的.是F2吗?从技术上讲,没有.该规范在6.5节中说明存在从lambda表达式到兼容委托类型的转换.那是一个lambda表达式吗?不.它是带括号的表达式,包含lambda表达式.
Visual C#编译器在此处进行小规格违规并丢弃括号.
第二:
int M() { return 1; }
Func<int> F3() { return M; }
Func<int> F4() { return (M); }
Run Code Online (Sandbox Code Playgroud)
F3是合法的.是F4吗?No. 7.5.3节规定括号表达式可能不包含方法组.同样,为方便起见,我们违反了规范并允许转换.
第三:
enum E { None }
E F5() { return 0; }
E F6() { return (0); }
Run Code Online (Sandbox Code Playgroud)
F5是合法的.是F6吗?不.规范声明存在从字面零到任何枚举类型的转换." (0)"不是字面零,它是一个括号后跟字面零,后跟括号.我们违反了这里的规范,实际上允许任何编译时常量表达式等于零,而不仅仅是字面零.
因此,在每种情况下,我们都允许您逃脱它,即使技术上这样做是非法的.
Vla*_*kov 40
当括号的存在可能对程序行为产生影响时,存在极端情况:
1.
using System;
class A
{
static void Foo(string x, Action<Action> y) { Console.WriteLine(1); }
static void Foo(object x, Func<Func<int>, int> y) { Console.WriteLine(2); }
static void Main()
{
Foo(null, x => x()); // Prints 1
Foo(null, x => (x())); // Prints 2
}
}
Run Code Online (Sandbox Code Playgroud)
2.
using System;
class A
{
public A Select(Func<A, A> f)
{
Console.WriteLine(1);
return new A();
}
public A Where(Func<A, bool> f)
{
return new A();
}
static void Main()
{
object x;
x = from y in new A() where true select (y); // Prints 1
x = from y in new A() where true select y; // Prints nothing
}
}
Run Code Online (Sandbox Code Playgroud)
3.
using System;
class Program
{
static void Main()
{
Bar(x => (x).Foo(), ""); // Prints 1
Bar(x => ((x).Foo)(), ""); // Prints 2
}
static void Bar(Action<C<int>> x, string y) { Console.WriteLine(1); }
static void Bar(Action<C<Action>> x, object y) { Console.WriteLine(2); }
}
static class B
{
public static void Foo(this object x) { }
}
class C<T>
{
public T Foo;
}
Run Code Online (Sandbox Code Playgroud)
希望你在实践中永远不会看到这一点.