C# Fundamentals - tutorial 3

Designed and Written by Michael Bartlett


This tutorial will demonstrate the four ways of passing data to and from class methods. In addition, we will also be looking at the four different types of iteration structures available in C#. There is only one code sample available for this tutorial, namely 3x1.cs. This program is broken down into five main components. Firstly, the body of the Main method, which in turn calls one of four methods - each one demonstrating a different type of loop and way of passing data.

USING LOOPS AND METHODS

After examining the listing for the Main method below, a number of things should be evident to you given what you have learned in the tutorials thus far. As usual, we begin by creating an instance of the program class, UsingLoopsAndMethods, which we call programInstance. You should then see that a label has been declared called getChoice which will act as a point in the program that we can return to should we need to execute any code below that label again. programInstance.ShowMenu() simply calls the method ShowMenu which, as you'll see in the code, displays the menu options to the user. To demonstrate the different types of loops, the program gives you the opportunity to select which one you would like to see in action. Once the menu is displayed, the user's input is captured, and the program uses a switch statement so that the relevant path of execution is taken.

The Main method (taken from listing 3x1.cs)

THE DO LOOP

The do loop is contained within the method DoLoop. The purpose of the do loop is to execute some code, then examine a condition, and then execute the code again if the condition is not met. This cycle will continue until the condition is met. The body of the DoLoop is shown below:

The DoLoop method (taken from listing 3x1.cs)

In this case, the do loop will only successfully exit once the user enters one of two words in lower case - these are 'quit' and 'exit'. The body of the loop simply displays a prompt and then receives user input. One very important thing to notice here is how the DoLoop method is structured. You'll notice that the parameter is passed in as an out. An out simply means that an unassigned reference must be passed in. The body of this method then assigns an actual value to the reference. A value must be assigned to an out parameter before the method returns. The following code, taken from Main, demonstrates how a method that receives an out parameter is called:

Using an out parameter (taken from listing 3x1.cs)

To see an example of how this all fits together, compile and run 3x1.cs. An example of using the do loop via this program is shown below. Here, we demonstrate the do loop by selecting it from the main menu. We defy the criteria required for exiting the loop a few times, before finally yielding:

>3x1

THE WHILE LOOP

The while loop is incredibly similar to the do loop. The only real difference is that the while loop evaluates its exit condition first, then executes the relevant code if the condition evaluates to true. As we know, the do loop processes its code first, then evaluates its criteria afterwards. The code shown in the while loop below should be easy to grasp:

The WhileLoop method (taken from listing 3x1.cs)

The method that contains the while loop - namely WhileLoop takes a ref parameter. This just means that a variable can be fed to the method, have its value changed, and the change will be reflected when the method is exited. The direct opposite of this is passing by value which means that a copy of the variable is made and passed to the method. This means any changes made to the variable in the method will not be reflected in the variable outside the method because these changes will be executed upon the copy (due to passing by value) rather than the actual variable itself (which is possible with ref).

This principal is demonstrated, firstly in the code below that makes use of the ref keyword when calling WhileLoop;

Using a ref parameter

and in the actual execution of the WhileLoop method:

>3x1

THE FOR LOOP

Since I've just explained the difference between passing by reference, and passing by value, I will now demonstrate the latter as we have not seen an example of this yet. The demonstration comes via the code for explaining C#'s for loop. As a programmer, the notion of a for loop should be something you are comfortable with. In C#, you need to specify three things in order to set a for loop up. The code below shows the method ForLoop. The first thing specified is the starting position for the loop. In this case, we are stating that the forcounter to equal one. We then specify the criteria that needs to be true in order for the loop to iterate. In this instance we are saying that the value of counter must be less than or equal to a_value. Finally we tell the for loop what will happen on every iteration - in this case we want the value in counter to increase by one. The value a_value is the variable that is passed by value; there are no special keywords here, as this is the default way of passing data in C#.

The ForLoop method (taken from listing 3x1.cs)

When selecting the for loop option from 3x1, the following output will be obtained:

>3x1

THE FOR-EACH LOOP

For-each has its roots in Visual Basic. The idea behind it is to allow a program to step through a collection of related objects in a standard sequential way, starting at the beginning and ending at the end. In the example program, the for-each technique is simply used to step through an array of strings, and print each one out. The keyword params is an input-only parameter; thus any changes made to the local copy inside the method will not effect the version outside. The only legal data types that can be used via the params keyword are single dimension or jagged arrays. This is why the strings that are passed in are delimited by commas.

The ForEach method (taken from listing 3x1.cs)

Finally running the program with the for-each option produces:

>3x1

SUMMARY


[OVERVIEW] [TUTORIAL 1] [TUTORIAL 2] [TUTORIAL 3] [TUTORIAL 4]