Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use the "return" statement in C# methods declared as void?

Tags:

c#

return

void

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?

like image 486
gonzobrains Avatar asked Feb 03 '26 17:02

gonzobrains


1 Answers

Yes it's definitely legal.

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

like image 178
dcaswell Avatar answered Feb 06 '26 05:02

dcaswell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!