Note about using return in the "default" switch case

in the video, @ben told us that he forgot to end the default switch case with a return.

switch (cameraRaycaster.layerHit) {
...
default:
   return; //caution here!
}

while in this example it may not be a problem, it would be better maybe to explain the difference between using return and break there.

the error the compiler was giving, was just that you can’t leave a default statement without a keyword that end the switch flow, so you must use break; goto; or return (but please, everybody, just forget about goto keyword :slight_smile:)

anyway the main difference between using break and return is that the break just ends the switch statement, while the return exit from the current method, so the code after the switch statement is never executed (and this may cause lot of problem if wasn’t done by purpose)

My 2 cents

switch ref: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch

1 Like

Thanks so much for this feedback. I think maybe to save adding to the confusion I’ll leave the video, but be sure to cover this next time we visit switch statements.

I hope you’re enjoying the course(s)

More on…C# Switch Case

Privacy & Terms