How to exit a method without exiting a program?

I'm still pretty new to C # and it's hard for me to get used to it compared to C / CPP.

How to exit a function in C # without exiting a program, how will this function be?

if (textBox1.Text == "" || textBox1.Text == String.Empty || textBox1.TextLength == 0) textBox3.Text += "[-] Listbox is Empty!!!!\r\n"; System.Environment.Exit(0); 

This will not allow returning types, and if they are left alone, it will continue to work without a function. Which is undesirable.

+41
methods c # return exit
Jul 22 '10 at 23:24
source share
6 answers

There are two ways to exit the method earlier (without exiting the program):

  • Use the return keyword.
  • Throw an exception.

Exceptions should be used only for exceptional circumstances - when the method cannot continue and it cannot return a reasonable value that makes sense to the caller. Usually, although you should just come back when you're done.

If your method returns void, you can write return without a value:

 return; 



In particular, about your code:

  • There is no need to write the same test three times. All these conditions are equivalent.
  • You should also use curly braces when you write the if statement so that it is clear which statements are inside the body of the if statement:

     if (textBox1.Text == String.Empty) { textBox3.Text += "[-] Listbox is Empty!!!!\r\n"; } return; // Are you sure you want the return to be here?? 
  • If you are using .NET 4, there is a useful method that, depending on your requirements, you can use here: String.IsNullOrWhitespace .

  • You could use Environment.Newline instead of "\r\n" .
  • You might want to consider a different way of displaying invalid input than writing messages in a text field.
+91
Jul 22 '10 at 23:25
source share
β€” -

In addition to the answer β€œMark”, you also need to know the area that (as in C / C ++) is indicated using curly braces. So:

 if (textBox1.Text == "" || textBox1.Text == String.Empty || textBox1.TextLength == 0) textBox3.Text += "[-] Listbox is Empty!!!!\r\n"; return; 

always comes back to this point. However:

 if (textBox1.Text == "" || textBox1.Text == String.Empty || textBox1.TextLength == 0) { textBox3.Text += "[-] Listbox is Empty!!!!\r\n"; return; } 

will be returned only if it is included in this if .

+6
Jul 22 2018-10-22T00:
source share

If the function is empty, the termination of the function will be return . Otherwise, you need to make an explicit return someValue . As Mark noted, you can also throw exception. What is the context of your question? Do you have a larger code example with which you can show some ways to exit this function?

0
Jul 22 '10 at 23:28
source share

The main problem is that you are mistaken in System.Environment.Exit for return.

0
Jul 23 '10 at 0:13
source share

@ John, Erlz and Nathan. As I learned about this in uni: functions return values, but methods do not. In some languages, the / syntax was actually different. Example (no specific language):

 Method SetY(int y) ... Function CalculateY(int x) As Integer ... 

Most languages ​​now use the same syntax for both versions, using void as the return type to say that it is not actually a return type. I guess this is because the syntax is more consistent and easier to change from method to function, and vice versa.

0
Jul 23 2018-10-10T00:
source share

I would use return null; to indicate that there is no data to return

0
Aug 6 '13 at 14:45
source share