1

Tell me please what is different between

return null;

and

return (null);

Sometime i'd seen each an examples

AleksP
  • 1,244
  • 2
  • 12
  • 18
  • Some related threads: [Parenthesis surrounding return values](http://stackoverflow.com/questions/161879/) (in C, not C#); [Does using parentheses around return values provide any compiler-related benefits?](http://stackoverflow.com/questions/20332928/) (C#); [Is there a reason to return null inside parentheses](http://stackoverflow.com/questions/24146739/) (C#, similar to your question). – Jeppe Stig Nielsen Aug 12 '14 at 10:04

3 Answers3

8

There's no difference in behaviour whatsoever - but the latter is distinctly unidiomatic. I've previously seen it as a sign that the author seems to think that return is a function call, or at least want to treat it as such. (Sometimes there are brackets around every return value.)

Just use the first version.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Maybe that author does not think it is a function call, he just knows that many statements (like `if (...)`, `while (...)`, `switch (...)`, `using (...)` and others, cf. comments to Patrick Hofman's answer) require parenthesis, and then he incorrectly assumes that the `return` statement also requires it. The designers of C# could have decided that the `return` statement required `()`, but they chose not to. – Jeppe Stig Nielsen Aug 12 '14 at 09:44
  • So actually we can close it as 'by design' :) – Patrick Hofman Aug 12 '14 at 09:45
6

The parenthesis serve no purpose.

Those two statements are exactly the same when you look to the actual IL code generated.

According to the specs, the () operator serve two purposes:

  • Specify casts, or type conversions, like

    (int)4M;
    
  • Invoke methods or delegates, like

    Method();
    

Parenthesis are also used to indicate (a group of) conditions conditions, like:

if (a && (b || c))

Or:

switch (a)

Or to specify the operator precedence (deviating from the default):

(a + b) * c

Lamdba expressions:

(x) => { }

Etc, etc.

Your code is neither of them, so they are useless.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 2
    actually, there are lot of more uses for parenthesis... – vidstige Aug 12 '14 at 08:47
  • @vidstige: I will be happy to add them to the list. Can you name them? – Patrick Hofman Aug 12 '14 at 08:47
  • 3
    changing away from implicit operator precedence would be an important one; is `a + b * c` the same as `(a + b) * c` or `a + (b * c)`? bundling expressings - `foreach`, `switch`, `catch` etc all use parenthesis – Marc Gravell Aug 12 '14 at 08:49
  • @MarcGravell: Thanks. Added it including link to MSDN. – Patrick Hofman Aug 12 '14 at 08:55
  • There are many statements in C# which require (or may take) an expression where the parenthesis `()` is required. For example it is not allowed to say `if condition { Work(); }` or `while condition { Work(); }`. @MarcGravell's example with `switch` is similar. `catch` may or may not specify a type or variable declaration (neither is a usual expression), and the parenthesis is required resp. illegal in the two cases, so `catch FileNotFoundException { HandleError(); }` is bad, as is `catch () { HandleError(); }`. – Jeppe Stig Nielsen Aug 12 '14 at 09:39
  • @JeppeStigNielsen: You are right. I think it is the wrong place to make an exhausted list of all uses, but the information might be useful to OP. – Patrick Hofman Aug 12 '14 at 09:41
  • @JeppeStigNielsen: Updated the answer so include a few other samples. – Patrick Hofman Aug 12 '14 at 09:44
  • @AleksP: Need more help or clarification? – Patrick Hofman Aug 12 '14 at 10:10
1

It's the same difference as between

var a = (new A());

and

var a = new A();

namely, no differene at all. The parenthesis is redundant and should be omitted. Some feel it makes the code more coherent with method calls, however the return statement is not a function call.

vidstige
  • 12,492
  • 9
  • 66
  • 110