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

CHAPTER 5 METHODS

Stack Frames

So far, you know that local variables and parameters are kept on the stack. Let’s look at that organization a little further.

When a method is called, memory is allocated at the top of the stack to hold a number of data items associated with the method. This chunk of memory is called the stack frame for the method.

The stack frame contains memory to hold the following:

The return address—that is, where to resume execution when the method exits

Those parameters that allocate memory—that is, the value parameters of the method, and the parameter array if there is one

Various other administrative data items relevant to the method call

When a method is called, its entire stack frame is pushed onto the stack.

When the method exits, its entire stack frame is popped from the stack. Popping a stack frame is sometimes called unwinding the stack.

For example, the following code declares three methods. Main calls MethodA, which calls MethodB, creating three stack frames. As the methods exit, the stack unwinds.

class Program

{

static void MethodA( int par1, int par2)

{

Console.WriteLine("Enter

MethodA: {0}, {1}", par1, par2);

MethodB(11, 18);

// Call MethodB.

Console.WriteLine("Exit

MethodA");

}

static void MethodB(int par1, int par2)

{

Console.WriteLine("Enter MethodB: {0}, {1}", par1, par2); Console.WriteLine("Exit MethodB");

}

static void Main( )

{

Console.WriteLine("Enter Main");

MethodA( 15, 30);

// Call MethodA.

Console.WriteLine("Exit

Main");

}

 

}

104

CHAPTER 5 METHODS

This code produces the following output:

Enter

Main

Enter

MethodA: 15, 30

Enter

MethodB: 11, 18

Exit

MethodB

Exit

MethodA

Exit

Main

 

 

Figure 5-14 shows how the stack frames of each method are placed on the stack when the method is called and how the stack is unwound as the methods complete.

Figure 5-14. Stack frames in a simple program

105

CHAPTER 5 METHODS

Recursion

Besides calling other methods, a method can also call itself. This is called recursion.

Recursion can produce some very elegant code, such as the following method for computing the factorial of a number. Notice that inside the method, the method calls itself with an actual parameter of one less than its input parameter.

int Factorial(int inValue)

 

{

 

 

 

 

 

if (inValue <= 1)

 

 

return inValue;

 

 

else

 

 

return inValue * Factorial(inValue - 1);

// Call Factorial again.

}

 

 

 

Calls itself

The mechanics of a method calling itself are exactly the same as if it had called another, different method. A new stack frame is pushed onto the stack for each call to the method.

For example, in the following code, method Count calls itself with one less than its input parameter and then prints out its input parameter. As the recursion gets deeper, the stack gets larger.

class Program

{

public void Count(int inVal)

{

if (inVal == 0) return;

Count(inVal - 1); // Invoke this method again.

Calls itself

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

}

static void Main()

{

Program pr = new Program(); pr.Count(3);

}

}

This code produces the following output:

1

2

3

106

CHAPTER 5 METHODS

Figure 5-15 illustrates the code. Notice that with an input value of 3, there are four different, independent stack frames for method Count. Each has its own value for input parameter inVal.

Figure 5-15. Example of recursion

107

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