Previous Page
Next Page

Declaring Methods

A method is a named sequence of statements. If you have previously programmed using languages such as C or Visual Basic, a method is very similar to a function or a subroutine. Each method has a name and a body. The method name should be a meaningful identifier that indicates the overall purpose of the method (CalculateIncomeTax, for example). The method body contains the actual statements to be run when the method is called. Most methods can be given some data for processing and can return information, which is usually the result of the processing. Methods are a fundamental and powerful mechanism.

Specifying the Method Declaration Syntax

The syntax of a Microsoft Visual C# method is as follows:

returnType methodName ( parameterList )  
{  
    // method body statements go here  
}
IMPORTANT
C, C++, and Microsoft Visual Basic programmers should note that C# does not support global methods. You must write all your methods inside a class, or your code will not compile.

Here's the definition of a method called addValues that returns an int result and has two int parameters called leftHandSide and rightHandSide:

int addValues(int leftHandSide, int rightHandSide)  
{  
    // ... 
    // method body statements go here  
    // ... 
}

Here's the definition of a method called showResult that does not return a value and has a single int parameter called answer:

void showResult(int answer) 
{ 
    // ... 
}

Notice the use of the keyword void to indicate that the method does not return anything.

IMPORTANT
Visual Basic programmers should notice that C# does not use different keywords to distinguish between a method that returns a value (a function) and a method that does not return a value (a procedure or subroutine). You must always specify either a return type or void.
Writing return Statements

If you want a method to return information (in other words, its return type is not void), you must write a return statement inside the method. You do this using the keyword return followed by an expression that calculates the returned value and a semicolon. The type of expression must be the same as the type specified by the function. In other words, if a function returns an int, the return statement must return an int; otherwise your program will not compile. Here is an example:

int addValues(int leftHandSide, int rightHandSide)  
{  
    // ...  
    return leftHandSide + rightHandSide; 
}

The return statement should be at the end of your method because it causes the method to finish. Any statements after the return statement are not executed (though the compiler warns you about this problem if you put statements after the return statement).

If you don't want your method to return information (in other words, its return type is void), you can use a variation of the return statement to cause an immediate exit from the method. You write the keyword return, immediately followed by a semicolon. For example:

void showResult(int answer)  
{  
    // display the answer 
    ... 
    return; 
}

If your method does not return anything, you can also omit the return statement, because the method finishes automatically when execution arrives at the closing curly brace at the end of the method. Although this practice is common, it is not always considered good style.

In the following exercise, you will examine another version of the MathsOperators application from Chapter 2. This version has been improved by the careful use of some small methods.

TIP
There is no minimum size for a method. If a method helps to avoid repetition and makes your program easier to understand, the method is useful regardless of how small it is.

There is also no maximum line length for a method, but usually you want to keep your method code small enough to get the job done. If your method is more than one screen in length, consider breaking it into smaller methods for readability.
Examine method definitions
  1. Start Visual Studio 2005.

  2. Open the Methods project in the \Microsoft Press\Visual CSharp Step by Step\Chapter 3\Methods folder in your My Documents folder.

  3. On the Debug menu, click Start Without Debugging.

    Visual Studio 2005 builds and runs the application.

  4. Re-familiarize yourself with the application and how it works, and then click Quit.

  5. Display the code for Form1.cs in the Code and Text Editor window (right-click Form1.cs in the Solution Explorer and click View Code).

  6. In the Code and Text Editor window, locate the addValues method.

    The method looks like this:

    private int addValues(int leftHandSide, int rightHandSide)
    {
        expression.Text = leftHandSide.ToString() + " + " + rightHandSide.ToString();
        return leftHandSide + rightHandSide; 
    }

    The addValues method contains two statements. The first statement displays the calculation being performed in the expression TextBox on the form. The values of the parameters leftHandSide and rightHandSide are converted into strings (using the ToString method you met in Chapter 2), and concatenated together with a “+” sign in the middle.

    The second statement uses the + operator to add the values of the leftHandSide and rightHandSide int variables together and returns the result of this addition. Remember that adding two int values together creates another int value, so the return type of addValues is int.

  7. In the Code and Text Editor window, locate the showResult method.

    The showResult method looks like this:

    private void showResult(int answer)
    {
        result.Text = answer.ToString();
    }

    This method contains one statement that displays a string representation of the answer parameter in the result TextBox.


Previous Page
Next Page