Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 5 METHODS

Return Values

A method can return a value to the calling code. The returned value is inserted into the calling code at the position in the expression where the invocation occurred.

To return a value, the method must declare a return type before the method name.

If a method doesn’t return a value, it must declare a return type of void.

The following code shows two method declarations. The first returns a value of type int. The second doesn’t return a value.

Return type

int GetHour() { ... } void DisplayHour() { ... }

No value is returned.

A method that declares a return type must return a value from the method by using the following form of the return statement, which includes an expression after the keyword return. Every path through the method must end with a return statement of this form.

return Expression;

// Return a value.

 

 

 

Evaluates to a value of the return type

 

For example, the following code shows a method called GetHour, which returns a value of type int.

Return type

 

 

 

 

 

int GetHour( )

 

 

{

DateTime dt = DateTime.Now;

// Get the current date and time.

 

 

 

 

int hour

= dt.Hour;

// Get the hour.

 

 

return hour;

 

// Return an int.

}

 

 

 

Return statement

76

CHAPTER 5 METHODS

You can also return objects of user-defined types. For example, the following code returns an object of type MyClass:

Return type -- MyClass

MyClass method3( )

{

MyClass mc

= new MyClass();

 

...

 

 

 

return

mc;

 

// Return a MyClass object.

}

As another example, in the following code, method GetHour is called in the WriteLine statement in Main and returns an int value to that position in the WriteLine statement.

class MyClass

 

 

{

↓ Return type

 

public int GetHour()

 

{

DateTime dt = DateTime.Now;

// Get the current date and time.

 

 

int hour

= dt.Hour;

// Get the hour.

 

return hour;

 

// Return an int.

}

 

 

 

 

}Return value

class Program

 

 

 

 

{

 

 

 

 

static void Main()

Method invocation

{

MyClass mc = new MyClass();

 

 

 

Console.WriteLine("Hour: {0}", mc.GetHour());

}

 

}

Instance

Method

 

name

name

77

CHAPTER 5 METHODS

The Return Statement and Void Methods

In the previous section, you saw that methods that return a value must contain return statements. Void methods do not require return statements. When the flow of control reaches the closing curly brace of the method body, control returns to the calling code, and no value is inserted back into the calling code. Often, however, you can simplify your program logic by exiting the method early when certain

conditions apply.

You can exit from a void method at any time by using the following form of the return statement, with no parameters:

return;

This form of the return statement can be used only with methods declared void.

For example, the following code shows the declaration of a void method called SomeMethod, which has three possible places it might return to the calling code. The first two places are in branches called if statements, which are covered in Chapter 9. The last place is the end of the method body.

Void return type

 

void SomeMethod()

 

{

 

...

 

if ( SomeCondition )

// If ...

return;

// return to the calling code.

...

 

if ( OtherCondition )

// If ...

return;

// return to the calling code.

...

 

}

// Default return to the calling code.

78

CHAPTER 5 METHODS

The following code shows an example of a void method with a return statement. The method writes out a message only if the time is after noon. The process, which is illustrated in Figure 5-5, is as follows:

First the method gets the current date and time. (Don’t worry about understanding the details of this right now.)

If the hour is less than 12 (that is, before noon), the return statement is executed, and control immediately returns to the calling method without writing anything to the screen.

If the hour is 12 or greater, the return statement is skipped, and the code executes the WriteLine statement, which writes an informative message to the screen.

class MyClass

 

 

 

 

 

 

 

{

Void return type

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

void TimeUpdate()

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DateTime dt = DateTime.Now;

 

// Get the current date and time.

 

 

if (dt.Hour < 12)

 

// If the hour is less than 12,

 

 

 

 

return;

 

// then return.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Return to calling method.

 

 

 

 

 

 

 

 

Console.WriteLine("It's afternoon!");

 

 

// Otherwise, print message.

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

static void Main()

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MyClass mc = new MyClass();

// Create an instance of the class.

 

mc.TimeUpdate();

// Invoke the method.

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 5-5. Using a return statement with a void return type

79

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]