75

I am still pretty new to C# and am having a difficult time getting used to it compared to C/CPP.

How do you exit a function on C# without exiting the program like this function would?

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 return types and if left alone it will keep going on through the function unstopped. Which is undesirable.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Nightforce2
  • 1,426
  • 5
  • 18
  • 31
  • 1
    BTW, they're called "methods", not "functions" these days. – John Saunders Jul 22 '10 at 23:31
  • 3
    @John I've been programming in C# and other OOP languages for a good time now and I still call them functions. (and I'm only 19!) – Earlz Jul 22 '10 at 23:40
  • @Earlz: maybe it's just me, but I find that most people who still say "function", or "subroutine" don't know OO. Therefore, hearing someone say "function" gives me the impression they don't know OO. – John Saunders Jul 22 '10 at 23:43
  • Every method is a function, and is also a subroutine (but not every function or subroutine is a method). In any case, in my favorite language it's called "member functions", and that's the way I likes them! ;) – Pavel Minaev Jul 22 '10 at 23:54
  • 4
    @John, a method, to me, implies operating on an instance. A function operates on its arguments, regardless of whether or not it is static. A method is a sub-case of functions that operate on an instance, where there the instance is the first argument (implied or explicit, depending on your language). – Nathan Ernst Jul 22 '10 at 23:59
  • 1
    @Nathan: I prefer "instance method" and "static method" or "class method". Functions are a non-OO concept, and don't refer to the concept of a class. – John Saunders Jul 23 '10 at 00:43
  • 1
    To most the difference between "method" and "function" is like the difference between "armor" and "armour" or "color" and "colour". It depends on how you look at it. – ThrowingDwarf Jun 17 '16 at 09:36
  • Methods can be distinguished from functions with the rule that they require the runtime type of an argument or arguments (multi-methods) to determine how dispatch them. One could also say a method is encapsulated by a class or object, which would accommodate static methods and javascript prototypes, but might exclude multi-methods. The first would allow "function" to be used for "statically dispatched" execution bits; the second would only allow for "bare" execution bits (not possible in Java, but certainly so in c# and javascript, c++ and c). – Gerard ONeill Jul 24 '23 at 18:29

6 Answers6

168

There are two ways to exit a method early (without quitting the program):

  • Use the return keyword.
  • Throw an exception.

Exceptions should only be used for exceptional circumstances - when the method cannot continue and it cannot return a reasonable value that would make sense to the caller. Usually though you should just return when you are done.

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

return;

Specifically about your code:

  • There is no need to write the same test three times. All those conditions are equivalent.
  • You should also use curly braces when you write an 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 might want to consider using here: String.IsNullOrWhitespace.

  • You might want to use Environment.Newline instead of "\r\n".
  • You might want to consider another way to display invalid input other than writing messages to a text box.
Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
9

In addition to Mark's answer, you also need to be aware of scope, which (as in C/C++) is specified using braces. So:

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

will always return at that point. However:

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

will only return if it goes into that if statement.

Smashery
  • 57,848
  • 30
  • 97
  • 128
3

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

beaumondo
  • 4,862
  • 7
  • 29
  • 42
1

The basic problem here is that you are mistaking System.Environment.Exit for return.

Maxime
  • 8,645
  • 5
  • 50
  • 53
Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45
0

If the function is a void, ending the function will return. Otherwise, you need to do an explicit return someValue. As Mark mentioned, you can also throw an exception.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

@John, Earlz and Nathan. The way I learned it at uni is: functions return values, methods don't. In some languages the syntax is/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 a return type to say there actually isn't a return type. I assume it's because the syntax is more consistent and easier to change from method to function, and vice versa.