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

CHAPTER 5 METHODS

Method Invocations

You can call other methods from inside a method body.

The phrases call a method and invoke a method are synonymous.

You call a method by using its name, along with the parameter list, which I’ll discuss shortly.

For example, the following class declares a method called PrintDateAndTime, which is called from inside method Main:

class MyClass

 

 

 

 

{

 

 

 

 

 

void PrintDateAndTime( )

// Declare the method.

{

DateTime dt = DateTime.Now;

// Get the current date and time.

 

}

Console.WriteLine("{0}", dt);

// Write it out.

 

 

 

 

 

static void Main()

 

 

 

// Declare the method.

{

 

 

 

 

 

 

MyClass mc = new MyClass();

 

 

mc.PrintDateAndTime( );

// Invoke the method.

}

 

 

 

}

Method

Empty

 

 

name

parameter list

 

Figure 5-4 illustrates the sequence of actions when a method is called:

1.Execution of the current method suspends at that point of the invocation.

2.Control transfers to the beginning of the invoked method.

3.The invoked method executes until it completes.

4.Control returns to the calling method.

Figure 5-4. Flow of control when calling a method

75

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