3

I was looking at some MSDN Documentation on IXmlSerializable and the Example Code listed the following method:

public XmlSchema GetSchema()
{
    return(null);
}

Is there a semantic difference between return null and return (null), ie does it invoke some compiler optimization, or is it purely stylistic?

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
  • 1
    Why would we know any more than you why the author added a superfluous set of parenthesis? – Servy Feb 02 '15 at 20:51
  • 1
    maybe the author was used to a language where `return` was a method? – DLeh Feb 02 '15 at 20:52
  • 2
    @DLeh: i'd be suprised if microsoft let someone write the documentation from somone who is not familiar with that language. – Tim Schmelter Feb 02 '15 at 20:54
  • So the answer is: the syntax is legal and they wanted to write it that way – Jonesopolis Feb 02 '15 at 20:55
  • 1
    @TimSchmelter I would be too :P. Half-joking reply. I hadn't realized you could have a `return` without a space after it, it sure does *look* like a method like this. – DLeh Feb 02 '15 at 20:55
  • Is there an advantage to writing this? A compiler optimization? I originally thought it might be some kind of casting syntax, but that doesn't seem to be the case. – Philip Pittle Feb 02 '15 at 20:56
  • 1
    @Servy - I wasn't asking for a critique on the author's style, but rather if this had semantic /syntactic meaning, ie invoking a compiler optimization. – Philip Pittle Feb 02 '15 at 20:59
  • The generated IL is exactly the same, so there is no difference from the compiler's point of view. – D Stanley Feb 02 '15 at 21:00
  • 2
    @PhilipPittle: compiler optimizations have nothing to do with the syntax you use, otherwise that would be clearly a compiler bug. – Tim Schmelter Feb 02 '15 at 21:01
  • @PhilipPittle You said right in the question that there's no semantic difference, and you were of course correct about that. What else does that leave? – Servy Feb 02 '15 at 21:01
  • 1
    apperantely whoever wrote this does not like to use space in `return` statement. there is also a `return(personName)` in `ToString` method. so this just seems like a matter of style. – Selman Genç Feb 02 '15 at 21:06
  • Personal preference. I usually do it when I have to return something like "return (a + b)" but never like return(a)" – Bauss Feb 02 '15 at 21:10
  • @PhilipPittle I think it is implied with "seem", but since not obvous to others, I'd suggest rewording to take out the statement " doesn't seem to be any difference" and change that statement to ask "Is there any semantic difference?" – AaronLS Feb 02 '15 at 21:16
  • 1
    @PhilipPittle And remove the "Why is the ..." question because that invites opinionated speculation about the original authors intention. – AaronLS Feb 02 '15 at 21:17
  • 1
    You might find this question interesting: http://stackoverflow.com/questions/2186595/is-there-a-difference-between-return-myvar-vs-return-myvar/2187498#2187498 – Eric Lippert Feb 02 '15 at 22:51

3 Answers3

4

Is there a semantic difference between return null and return (null)

As a whole statement, there isn't any difference. In this case the parentheses are redundant.

does it invoke some compiler optimization

Not at all.

is it purely stylistic?

Most likely, the example comes from an auto-generated code. In such scenarios, when generating an arbitrary expression the generator doesn't know in which context the expression will be used. Hence the easiest thing to do to protect the original intent is wrapping the expression being generated into parentheses. This makes sure it is understood as an 'atomic subexpression' in its surrounding context.

Consider the generator would output a+b and the caller would need to multiply it by c. The wrong way would be to plainly concatenate a+b and *c leading to a+b*c which is obviously wrong. However, when the nested generator returns (a+b), the whole construct is working as intended without any complex decision-making: (a+b)*c.

Note that modern code genrators usually work on expression trees with multi-pass processing instead of passing strings around, so it is much easier for them to eliminate redundant parentheses, making the result more readable by us, humans.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • 1
    You think that this MSDN code sample is auto-generated? – JLRishe Feb 02 '15 at 21:58
  • 1
    @JLRishe Yes, could be. Maybe just the method in question, which could have been auto-generated by an IDE feature helping the developer implement `IXmlSerializable`. – Ondrej Tucny Feb 02 '15 at 22:03
1

No difference, no compiler optimization.

See also e.g. Does using parentheses around return values provide any compiler-related benefits?

Community
  • 1
  • 1
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
1

As a general rule, if two statements are semantically equivalent (but syntactically different), any proper compiler will apply the same optimization to both of them. Since C# doesn't specify any difference between the meaning of return null; and return(null);, any hypothetical compiler optimizations that would apply to one would apply to the other.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139