Exiting Methods

Ordinarily, code within a method executes from beginning to end—literally. However, when a return statement is reached, execution immediately returns to the statement that made the method call; you can force execution to leave the method at any time by using a return statement. If Visual C# .NET encounters a return statement, the method terminates immediately, and code returns to the statement that called the method.

Avoiding Infinite Recursion

It's possible to call methods in such a way that a continuous loop occurs. Consider the following procedures:

public static void DoSomething()
{
    DoSomethingElse();
}
public static void DoSomethingElse()
{
   DoSomething();
}

Calling either of these methods produces an infinite loop of methods ...

Get Sams Teach Yourself Microsoft® Visual C#™ .NET in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.