• Tidak ada hasil yang ditemukan

METHODS

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 151-155)

9 MORE ABOUT CLASSES

Problem 12: Validate ISBN

9.4 METHODS

In the explanation of classes I have already dealt with methods, but there are a few concepts that you should be aware of. As mentioned, a method is identified by its name and the parameter list. The parameters that you specify in the method definition, are called the

I have shown how to specify that a method has a variable number of parameters, which is really just a question that the compiler creates an array as the actual parameter. Methods parameters can generally be of any type, but you should be aware that primitive types and class types are treated differently. For primitive types the transmitted values are directly copied, and that is, that on the stack are created a copy of the parameters and the values of the actual parameters are copied to these copies. This means that if a method is changing the value of a parameter that has a primitive type, then it is the value of the copy on the stack that is changed, and after the method is terminated, then the values of the calling code are unchanged. Wee therefore also call a parameter of a primitive type for a value parameter. What a primitive type really is is explained later in book C# 3.

Class parameters are in principle transferred in the same way, where there on the stack is created a copy of the parameter and the current value is copied to this. However, you should be aware of what is being created and copied. In the case of a reference parameter what is created on the stack is a reference, and what is copied is the reference to the current object.

Although parameters of primitive type and classes are treated slightly differently, the parameter transfer in C# is basic value transfers, which means that if a method change the value of a parameter, the change is unknown outside the method. A good example is a swap method, and thus a method that must reverses the two values. Consider the following method

public static void Swap(int a, int b) {

int t = a;

a = b;

b = t;

}

which has two parameters of the type int. If the method is executed as follows:

static void Main(string[] args) {

int t1 = 2;

int t2 = 3;

Swap(t1, t2);

Console.WriteLine(t1 + “ “ + t2);

}

the last statement prints

2 3

and the two numbers are not reversed. When the method Swap() is called, the variables t1 and t2 are transferred as actual parameters. The method Swap() has two parameters and a local variable, and has therefore three fields that are allocated on the stack, and the values of the actual parameters are copied to the stack:

The Swap() method works in that it copies the value of the parameter a to the local variable t, and then copy the value of b to a. Finally the value of t is copied to b, and then the content of the stack is as shown below.

This means that the two values are reversed, but it happened on the stack, and after the method is terminated the three elements are removed from the stack, and the changes are lost. This means that the variables in the calling code is unchanged.

However, C# supports reference parameters where the changes a method makes to the parameters will affect the actual parameters. The method Swap() could be written as:

public static void Swap(ref int a, ref int b) {

int t = a;

a = b;

b = t;

}

and the only difference is that each parameter is preceded by the word ref. If you then executes the method

Swap(ref t1, ref t2);

Console.WriteLine(t1 + “ “ + t2);

you will see that the values of the two variables t1 and t2 are exchanged. You must note that also the actual parameters must be preceded by the word ref. This time it is the variables addresses (references), which are copied to the stack, and the method Swap() knows that a and b must be interpreted as references to other variables. The result is, that it is the two variables in the Main() method which are changed.

Note that one can combine value parameters and reference parameters so that a method can have parameters for both reference and value transfer. Also note that a method can be overloaded for ref parameters, and the program SwapValues has two Swap() methods.

Methods have a type or they are void. The fact that a method is void means that it does not have a value, and it is therefore not required to have a return statement. As an example you have the method Add() in the class Student. A void method may well have an empty return statement, which then has the effect that the method is terminated. If a method has a type, it must have a return statement that returns a value of the same type as the method’s type. It is important to note that a method can only return one value, but the type of this value in turn can be anything, including a class type or an array. As an example, the method GetCourses() in the class Student returns a List<Course>, and precise, it is a reference to such an object.

As can be seen from the above, a method’s parameters are created when the method is called and initialized by the calling code. The parameters are removed again when the method terminates, and they live only while the method executes and they can only be used or referenced from the method itself. We say that the parameters scope are the method’s statements.

The same applies to the variables as a method might create. They are called local variables.

They are created when the method is called, and removed again when it terminates. Their scope is also limited to the method’s statements. A local variable can be created anywhere in the method, but they are all created, however when the method is called, but if a variable is referenced by a statement before it is defined, the compiler fails. In conclusion, a method can refer to

1. instance variables in the methods class 2. parameters

3. local variables

where, the last two have their scope limited to the method itself. By contrast, the scope of an instance variable is limited to the object, so that the variable live as long as the object does.

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 151-155)