Is there a difference between return myVar vs. return (myVar)?

前端 未结 4 1255
清酒与你
清酒与你 2020-12-07 14:19

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;
相关标签:
4条回答
  • 2020-12-07 14:56

    No, there is no difference other than syntactical.

    0 讨论(0)
  • 2020-12-07 15:01

    A good way to answer questions like this is to use Reflector and see what IL gets generated. You can learn a lot about compiler optimizations and such by decompiling assemblies.

    0 讨论(0)
  • 2020-12-07 15:09

    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.

    0 讨论(0)
  • 2020-12-07 15:15

    There are corner cases when presence of parentheses can have effect on the program behavior:

    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
        }
    }
    

    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
        }
    }
    

    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;
    }
    

    Hope you will never see this in practice.

    0 讨论(0)
提交回复
热议问题