- if Statements
- switch Statements
- C# Loops
- goto Statements
- break Statements
- continue Statements
- return Statements
- Summary
return Statements
return statements allow jumping out of a method or, in the case of the Main() method, the program. The following example shows how the return statement is used in the Main() method:
public static int Main(string[] args) { // other program statements return 0; }
The Main() method has a return type of int, as specified by the int declaration in front of the word Main. If the return value were void, there would be two choices: Don't use the return statement, or just use the statement return;" with no value. Because the example returns an int, the return statement must return an integer value. Therefore, when this program runs without problems and ends, it returns a value of 0 on the command line.
All methods have return types and have the same return statement options, as shown previously. The difference is that the value is returned to the statement making the method call. Listing 1 contains examples of most of the concepts covered in this article.
Listing 1: Program Flow Control Example
using System; /// <summary> /// This class allows a user to enter /// and print a list of web sites. /// </summary> public class WebSites1 { // Program entry public static int Main(string[] args) { string[] siteName = new string[6]; string phrase = "What is your pleasure"; string choice; int count = 0; // If there was a cmd line arg, use it. if (args.Length == 0) { Console.WriteLine("{0}, Master?", phrase ); } else { Console.WriteLine("{0}, {1}?", phrase, args[0]); } do { // Print menu. Console.WriteLine(""); Console.WriteLine("A - Add Site"); Console.WriteLine("S - Sort List"); Console.WriteLine("R - Show Report\n"); Console.WriteLine("Q - Quit\n"); Console.Write("Please Choose (A/S/R/Q): "); choice = Console.ReadLine(); // Figure out what user wanted. switch (choice) { // Add a site case "a": case "A": Console.WriteLine("\nAdding Site\n"); string doAgain = "Y"; // Keep it up as long as user wants while (doAgain.ToUpper() == "Y") { Console.Write( "Please Enter Site Name: "); siteName[count++] = Console.ReadLine(); Console.Write("Add Another?: "); doAgain = Console.ReadLine(); // There can only be 5 items if (count >= 5) { break; } } break; // Sort the site list case "s": case "S": Console.WriteLine("Sorting List..."); int n = siteName.Length-2; int j, k; string save; // Insertion sort, start at end & move up for (k=n-1; k >= 0; k--) { j = k + 1; save = siteName[k]; // Sentinel makes inner // loop more efficient siteName[n+1] = save; // Insert siteName[k] into // it's sorted position while ( String.Compare( save, siteName[j]) > 0 ) { siteName[j-1] = siteName[j]; j++; } siteName[j-1] = save; } // clean out sentinel so it's not printed siteName[siteName.Length-1] = null; Console.WriteLine("Done sorting."); break; // Print a report case "r": case "R": string filter = ""; string response = ""; // If user wants to filter, // get filter string Console.Write( "Would you like a Filter? "); response = Console.ReadLine(); if (response.ToUpper() == "Y") { Console.Write( "\nPlease enter a filter: "); filter = Console.ReadLine(); } Console.WriteLine(""); Console.WriteLine("Site Report"); Console.WriteLine(""); // Process every entry in siteName foreach(string site in siteName) { // Execute filter if (response.ToUpper() == "Y" && site != null && site.IndexOf(filter) == -1) { continue; } // Print non-filtered items Console.WriteLine("\t{0}", site); } break; // Exit Program case "q": case "Q": Console.WriteLine("GoodBye"); break; // User entered bad data default: Console.WriteLine("Huh??"); break; } // end switch // Keep going until user wants to quit } while ((choice=choice.ToUpper()) != "Q"); return 0; } }
The example above shows how to use most of the branching and looping statements in a working program. It has if and switch statements; while, do, for, and foreach loops; and break, continue, and return statements.