Tarek Saied
Tarek Saied

Reputation: 6626

What does RETURN in a method?

This is my first day using c#.

I think it is very easy :(.

What does return do in this case:

 if (flag == false)
        {
            MessageBox.Show("Not a number.Re-enter.");
            txtnum.Clear();
            txtnum.Focus();
            return;
        }

?

Upvotes: 1

Views: 1804

Answers (3)

Joe
Joe

Reputation: 82654

It simply exits the method. So that no code after that return will be executed.

Upvotes: 15

carlbenson
carlbenson

Reputation: 3207

Return is used as a means of stopping the execution of a method. Once hit, nothing else in the method will be processed. It's no different than a function that requires an int to be returned an ends when "return myInt" is hit. Here, your function returns void, so return alone, without a variable, is sufficient.

Upvotes: 4

Tod
Tod

Reputation: 8252

It bails out of whatever method you're in and returns to the caller.

Upvotes: 4

Related Questions