Information Technology Reference
In-Depth Information
For example, the following code shows a method called
MyMethod
, which takes two param-
eters—a variable of type
MyClass
and an
int
.
The method adds 5 to both the field of the class and the
int
.
You might also notice that
MyMethod
uses the modifier
static
, which you haven't seen
before. You can ignore it for now. I will talk about
static
methods in Chapter 6.
class MyClass
{ public int Val = 20; } // Initialize the field to 20.
class Program
Formal parameters
{
↓
static void MyMethod( MyClass f1, int f2 )
{
f1.Val = f1.Val + 5; // Add 5 to field of f1 param.
f2 = f2 + 5; // Add 5 to second param.
}
static void Main( )
{
MyClass A1 = new MyClass();
int A2 = 10;
MyMethod(
A1, A2
); // Call the method.
}
↑
}
Actual parameters