How to exit a method before ending in c#

Asked

Viewed 4,562 times

4

What should I do to stop the execution of a method before it ends, get out and continue the execution?

public void MEUMETODO()
{

   ...

   PARA E SAIR AQUI();

   ...


}
  • If the method returned something as it would?

  • It seems to me a valid question, if basic. Why vote against?

  • 1

    @Onosendai I’m not sure, but perhaps it’s "This question shows no research effort". A negative vote is not always meaning the question is useless (which in this case is not) or is not explicit.

  • @Precisely with the arming

1 answer

8


There are two ways to quit/finish the function:

  • Return
  • Throw

You can use a return; at any time to end the function:

public void MEUMETODO()
{    
   ...

   return;

   ...    
}

Or use the Throw when an exception occurs:

public void MEUMETODO()
{    
   try
   {
       ...
   }
   catch (Exception)
   {                    
       throw;
   }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.