Tải bản đầy đủ (.pdf) (4 trang)

Type perfect Object potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (21.54 KB, 4 trang )



Boxing
As you have just seen, variables of type object can refer to any object of any reference
type. That is, they can refer to any instance of a class. However, variables of type object
can also refer to a value type. For example, the following two statements initialize the
variable i (of type int, a value type) to 42 and then initialize the variable o (of type object,
a reference type) to i:
int i = 42;
object o = i;
The effect of the second statement is subtle. Remember that i is a value type and it exists
in the stack. If the reference inside o referred directly to i, the reference would be
referring to the stack. However, all references refer to objects on the heap. Instead, a
piece of memory is allocated from the heap, an exact copy of the value inside i is stored
in this piece of memory, and the reference inside o is pointed to the copy. (Creating
references to items on the stack could seriously compromise the robustness of the
runtime, and create a potential security flaw, so it is not allowed.) This automatic copying
of an item from the stack to the heap is called boxing. The following graphic shows the
result:

IMPORTANT
If you modify the original value of a variable, you are not modifying the value that now
exists on the heap, because it's just a copy.




Calling Methods
Methods exist to be called! You call a method by name to ask it to perform its task. If the
method requires information (as specified by its parameters), you must supply the
information requested. If the method returns information (as specified by its return type),


you should arrange to capture this information somehow.
Specifying the Method Call Syntax
The syntax of a C# method call is as follows:
methodName ( argumentList )
• The methodName must exactly match the name of the method you're calling.
Remember, C# is a case-sensitive language.
• The argumentList supplies the optional information that the method accepts. You
must supply an argument for each parameter, and the value of each argument must
be compatible with the type of its corresponding parameter. If the method you're
calling has two or more parameters, you must separate the arguments with
commas.
IMPORTANT
You must include the parentheses in every method call, even when calling a
method that has no arguments.
Here is the addValues method again:
int addValues(int leftHandSide, int rightHandSide)
{
//
}
The addValues method has two int parameters, so you must call it with two comma-
separated int arguments:
addValues(39, 3); // okay
You can also replace the literal values 39 and 3 with the names of int variables. The
values in those variables are then passed to the method as its arguments, like this:
int arg1 = 99;
int arg2 = 1;
addValues(arg1, arg2);
If you try to call addValues in some other way, you will probably not succeed, for the
reasons described in the following examples:
addValues; // compile time error, no parentheses

addValues(); // compile time error, not enough arguments
addValues(39); // compile time error, not enough arguments
addValues("39", "3"); // compile time error, wrong types
The addValues method returns an int value. This int value can be used wherever an int
value can be used. Consider these examples:
result = addValues(39, 3); // on right hand side of an assignment
showResult(addValues(39, 3)); // as argument to another method call
The following exercise continues using the MathsOperators application. This time you
will examine some method calls.
Examine method calls
1. Return to the Methods project. (This project is already open in Visual Studio 2005
if you're continuing from the previous exercise. If you are not, open it from the
\Microsoft Press\Visual CSharp Step by Step\Chapter 3\Methods folder in your
My Documents folder.
2. Display the code for Form1.cs in the Code and Text Editor window.
3. Locate the calculate_Click method, and look at the first two statements of this
method after the try statement and opening curly brace. (We will cover the
purpose of try statements in Chapter 6, “Managing Errors and Exceptions.”)
The statements are as follows:
int leftHandSide = System.Int32.Parse(leftHandSideOperand.Text);
int rightHandSide = System.Int32.Parse(rightHandSideOperand.Text);
These two statements declare two int variables called leftHandSide and
rightHandSide. However, the interesting parts are the way in which the variables
are initialized. In both cases, the Parse method of the System.Int32 class is called
(System is a namespace, Int32 is the name of the class in this namespace). This
method takes a single string parameter and converts it to an int value. These two
lines of code take whatever the user has typed into the leftHandSideOperand and
rightHandSideOperand TextBox controls on the form and converts them into int
values.
4. Look at the fourth statement in the calculate_Click method (after the if statement

and another opening curly brace):
calculatedValue = addValues(leftHandSide, rightHandSide));
This statement calls the addValues method, passing the values of the leftHandSide
and rightHandSide variables as its arguments. The value returned by the
addValues method is stored in the calculatedValue variable.
5. Look at the next statement:
showResult(calculatedValue);
This statement calls the showResult method, passing the value in the
calculatedValue variable as its argument. The showResult method does not return
a value.
6. In the Code and Text Editor window, find the showResult method you looked at
earlier.
The only statement of this method is this:
result.Text = answer.ToString();
Notice that the ToString method call uses parentheses even though there are no
arguments.
TIP
You can call methods belonging to other objects by prefixing the method with the
name of the object. In the previous example, the expression answer.ToString()
calls the method named ToString belonging to the object called answer.




Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×