2

While reading an MCSD study guide, I noticed the author said it was illegal to include a return statement in a method that declares the return type void. However, when I created the following method Visual Studio didn't flag it in the editor nor did it fail to compile:

private void ReturnNothing()
{
    return;
}

What is the real answer then? Is this legal?

gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • possible duplicate of [Is it bad practice to use return inside a void method?](http://stackoverflow.com/questions/1283325/is-it-bad-practice-to-use-return-inside-a-void-method) – Oliver Charlesworth Aug 26 '13 at 23:09
  • See http://stackoverflow.com/questions/1283325/is-it-bad-practice-to-use-return-inside-a-void-method for the answer, and reasons for & against it. – Adrian Wragg Aug 26 '13 at 23:09
  • Don't think it's an exact duplicate of the previous 2 questions. Not at all. – Mario Rossi Aug 26 '13 at 23:18
  • 1
    @MarioRossi - does not have to be *exact* duplicate... one studying for MCSD obviously looked it up if the statement is valid in C# specification, so seem to be perfectly fine to dup. – Alexei Levenkov Aug 26 '13 at 23:25
  • I don't want to post the actual name of the guide I was reading because I'm not sure if that is allowed and I don't want to accuse an author of publishing bad data. I am simply unsure what the real answer is because the guide says it is illegal but Visual Studio allows me to use it. – gonzobrains Aug 26 '13 at 23:33
  • @OliCharlesworth I am not asking about C# bad practice; I want to know if it is legal or not. I have used it before in C++ because I want to return from a method early. If it is allowed I will publish the name of the book and quote from it. – gonzobrains Aug 26 '13 at 23:35
  • @gonzobrains: The point is, the conclusion to that question was "no, it's not bad practice". Ergo, it can't be illegal ;) – Oliver Charlesworth Aug 26 '13 at 23:36
  • @OliCharlesworth I see how you would conclude that, but when performing a Google search that question didn't come up as a result, so I think my question is still beneficial and not an exact duplicate. Thanks for your contribution. – gonzobrains Aug 26 '13 at 23:48
  • I have added a link to the guide I am referring to. – gonzobrains Aug 26 '13 at 23:51
  • I'll add link I'm referring to too :) http://www.bing.com/search?q=msdn+c%23+return+statement : one of the top links - " If the method is a `void` type, the return statement can be omitted." – Alexei Levenkov Aug 27 '13 at 00:02
  • @AlexeiLevenkov bing...Bing...BING! – gonzobrains Aug 27 '13 at 00:08

2 Answers2

10

Yes it's definitely legal.

It's only illegal if you try to put a value after the return. This is wrong return 0;

dcaswell
  • 3,137
  • 2
  • 26
  • 25
0

The author was probably referring to the form

return value ;

which is effectively invalid when return type == void (but valid and required when return type != void).

Mario Rossi
  • 7,651
  • 27
  • 37
  • Actually, the author mentioned your scenario after the one to which I am referring. – gonzobrains Aug 26 '13 at 23:35
  • @MarioRossi I'm only on the fourth chapter and I have already found a handful of typos, but this error just seemed to important to ignore. This is why I decided to get opinions on SO. – gonzobrains Aug 27 '13 at 00:53
  • The *requirements* of a non-void method are that (1) all `return` statements have an expression convertible to the return type, and (2) the end point of the method is not reachable. This does not logically imply that a non-void method must have a return! `int Foo() { throw new NotImplementedException(); }` meets both requirements; zero out of a possible zero return statements have an expression convertible to `int`, and the end point is not reachable, so it is legal. – Eric Lippert Aug 27 '13 at 15:03
  • @EricLippert Yup, that's correct. How to present it to a person who is just learning C# without distracting him/her from the core of the explanation, I don't know. Perhaps through a good comment? :-) Thanks many, Eric. – Mario Rossi Aug 27 '13 at 15:18