This section expands on the explanation of declaring a method (such as Main()) to include any parameter or a return type. Listing 4.4 contains examples of these concepts, and Output 4.1 shows the results. Listing 4.4. Declaring a Method
Output 4.1.
Four methods are declared in Listing 4.4. From Main() the code calls GetUserInput(), followed by a call to GetFullName(). Both of these methods return a value and take parameters. In addition, the listing calls DisplayGreeting(), which doesn't return any data. No method in C# can exist outside the confines of an enclosing class. Even the Main method examined in Chapter 1 must be within a class.
Parameter DeclarationConsider the declaration of the DisplayGreeting() and GetFullName() methods. The text that appears between the parentheses of a method declaration is the parameter list. Each parameter in the parameter list includes the type of the parameter along with the parameter name. A comma separates each parameter in the list. Behaviorally, parameters are virtually identical to local variables, and the naming convention of parameters follows accordingly. Therefore, parameter names are camel case. Also, it is not possible to declare a local variable with the same name as a parameter of the containing method, because this would create two "local variables" of the same name.
Method Return DeclarationIn addition to GetUserInput() and GetFullName() requiring parameters to be specified, both of these methods also include a method return. You can tell there is a method return because a data type appears immediately before the method name of the method declaration. For both GetUserInput() and GetFullName(), the data type is string. Unlike parameters, only one method return is allowable. Once a method includes a return data type, and assuming no error occurs, it is necessary to specify a return statement for each code path (or set of statements that may execute consecutively) within the method declaration. A return statement begins with the return keyword followed by the value the method is returning. For example, the GetFullName() method's return statement is return firstName + " " + lastName. The C# compiler makes it imperative that the return type match the type of the data specified following the return keyword. Return statements can appear in spots other than at the end of a method implementation, as long as all code paths include a return if the method has a return type. For example, an if or switch statement at the beginning of a method implementation could include a return statement within the conditional or case statement; see Listing 4.5 for an example. Listing 4.5. A return Statement before the End of a Method
A return statement indicates a jump to the end of the method, so no break is required in a switch statement. Once the execution encounters a return, the method call will end. If particular code paths include statements following the return, then the compiler will issue a warning that indicates the additional statements will never execute. In spite of the C# allowance for early returns, code is generally more readable if there is a single exit location rather than multiple returns sprinkled through various code paths of the method. Specifying void as a return type indicates that there is no return from the method. As a result, the method does not support assignment to a variable or use as a parameter. Furthermore, the return statement becomes optional, and when it is specified, there is no value following the return keyword. For example, the return of Main() in Listing 4.4 is void and there is no return statement within the method. However, DisplayGreeting() includes a return statement that is not followed by any returned result.
|