|
Methods
Parameters
A value parameter is a parameter by which a value will be passed from the caller
into the called method. New value can be assigned to a value parameter,
but this value will not be returned to the caller at the end of the call.
Parameters of value based types are value parameters by default. Parameters
of reference based types can not be declared as value parameters.
A reference parameter is a parameter by which a reference will be passed from
the caller into the called method. The value referred by the parameter in the
caller can be used in the called method. If a new value is assigne to a reference
parameter in the called method, that value will replace the old value in the
caller referred by the parameter. Parameters of reference based types
are reference parameters by default. Parameters of value based types can be
declared as reference parameters using the "ref" modifier.
An output parameter is a parameter by which the new value assigned in the called
method will be returned to the caller, and replace the old value referred
by the parameter. However, at the time of entering the called method, values
in the caller referred by the output parameters will not be transferred in
the called method. Output parameters will must be declared using the "output"
modifier.
Let's play with different kinds of parameters in the following program:
using System;
class Parameters {
static void Main() {
int v, r, o;
string s;
v = 1;
s = "1";
r = 1;
o = 1;
MyMethod(v, s, ref r, out o);
Console.WriteLine("After calling: v = {0}",v);
Console.WriteLine("After calling: s = {0}",s);
Console.WriteLine("After calling: r = {0}",r);
Console.WriteLine("After calling: o = {0}",o);
}
static void MyMethod(int v, string s, ref int r, out int o) {
Console.WriteLine("In method: v = {0}",v);
Console.WriteLine("In method: s = {0}",s);
Console.WriteLine("In method: r = {0}",r);
// Console.WriteLine("In method: o = {0}",o); // o is undefined
v = 2;
s = "2";
r = 2;
o = 2;
}
}
Output:
In method: v = 1
In method: s = 1
In method: r = 1
After calling: v = 1
After calling: s = 1
After calling: r = 2
After calling: o = 2
I thought "string" is a reference type by default. But I was wrong. This program shows
that a "string" parameter is a value parameter.
Parameter Arrays
A parameter array is a paramter by which an undefined number parameters can
be passed into the called method from the caller. A parameter must be an array
type with the "params" modifier. The caller can supply an array, one value, two
values or any number of values for a parameter array. A parameter array must be
the last parameter of a method.
The following program shows the differences between a normal array parameter
a special parameter array:
using System;
class ParameterArray {
static void Main() {
int i1, i2;
int[] a = new int[2];
Console.WriteLine("Testing array paramenter...");
i1 = 11;
i2 = 12;
a[0] = 11;
a[1] = 12;
AParameter(a);
// AParameter(i1, i2); //an array parameter can only take an array
Console.WriteLine("After calling: a[0] = {0}",a[0]);
Console.WriteLine("After calling: a[1] = {0}",a[1]);
Console.WriteLine("Testing parameter array...");
PArray(a);
PArray(i1,i2);
Console.WriteLine("After calling: a[0] = {0}",a[0]);
Console.WriteLine("After calling: a[1] = {0}",a[1]);
Console.WriteLine("After calling: i1 = {0}",i1);
Console.WriteLine("After calling: i2 = {0}",i2);
}
static void AParameter(int[] a) {
Console.WriteLine("In method: a[0] = {0}",a[0]);
Console.WriteLine("In method: a[1] = {0}",a[1]);
a[0] = 21;
a[1] = 22;
}
static void PArray(params int[] a) {
Console.WriteLine("In method: a[0] = {0}",a[0]);
Console.WriteLine("In method: a[1] = {0}",a[1]);
a[0] = 31;
a[1] = 32;
}
}
Output:
Testing array paramenter...
In method: a[0] = 11
In method: a[1] = 12
After calling: a[0] = 21
After calling: a[1] = 22
Testing parameter array...
In method: a[0] = 21
In method: a[1] = 22
In method: a[0] = 11
In method: a[1] = 12
After calling: a[0] = 31
After calling: a[1] = 32
After calling: i1 = 11
After calling: i2 = 12
Note that if you are calling parameter array with an array, it will be used as
a reference parameter. If you are calling parameter array with a list of elements,
and the elements are value based types, they will be used a value parameters.
|