nuclear sweet
nuclear sweet

Reputation: 1121

Is there a reason to return null inside parentheses

I found return(null) in

http://msdn.microsoft.com/ru-ru/library/system.xml.serialization.ixmlserializable.aspx

I wonder is there some reason for this parentheses ? Why not just return null?

Upvotes: 3

Views: 819

Answers (3)

cHao
cHao

Reputation: 86506

In C#, the expressions foo and (foo) evaluate to exactly the same thing. The parens have no effect when the expression only has one term.

I actively prefer not using them, though, for a couple of reasons:

  • To me, at first glance, return(null) resembles a function call just a little too closely.
  • They hint at complexity that isn't there, and people -- even people who know better, but are just having a brain fart -- end up asking questions about what's special about that statement. This very question can be considered evidence.

If you do use them, you should probably stick a space after the return.

Upvotes: 0

TheNorthWes
TheNorthWes

Reputation: 2739

Hmm this was a curious question so I did some browsing.

I found this post which was posted answered by Jon Skeet. He states sometimes it increases readability but has no performance or logical impact.

Another user suggests it is a hold over from long ago when some compilers for C required them.

Interesting to see an example on MSDN with it though, nice find.

MSDN also has this

Many programmers use parentheses to enclose the expression argument of the return statement. However, C does not require the parentheses.

Upvotes: 4

dotnetom
dotnetom

Reputation: 24901

There is no reason for that, you can just as easily type

return null;

Upvotes: 3

Related Questions