C# proper to use return when...

techmanbd

Junior Contributor
Joined
Sep 10, 2003
Messages
397
Location
Burbank, CA
I was wondering if it is proper to use the return in a void method to get of of the method as you would use exit sub in VB. It works I know that, but is it the proper way?

For example

VB
Visual Basic:
private sub WWWW()

   if intL = 0 then exit sub
   ' other code here is intL not 0

End Sub

C#:
private void WWWW()
{
  if(intL == 0)
  {
     return;
   }
// other code here if intL not 0 
}
 
Ok thanks, I am learning the C# language and read the following in the help files. But after reading it again, I realized that that goes for the expression after the return word. Duh. :o

expression
The value returned by a method. The expression is not used with methods of the type void.
 
Some people, probably not many, equate using a return statement as a control-flow structure with using a goto statement: a mortal sin these days. (In other words, a return statement should only be used as the last statement in a non-void method.) I say use whatever makes your code the simplest. If that's a return in the middle of a function, fine by me.
 
Back
Top