Variables | specification
A variable represents a storage location. Each variable belongs to a type that determines what value can be stored in the variable. A local variable is a variable declared in a method, property, or indexer. A local variable is defined by specifying the type name and the declaration character, which specifies the variable name and optional initial value, such as:
int A;
int b = 1;
However, a local variable declaration can also contain more than one declaration character. The declarations of a and B can be overridden as:
int A, b = 1;
A variable must be assigned before its value can be used. Example
Class Test
{
static void Main () {
int A;
int b = 1;
int C = a + B; Error, a not yet assigned
...
}
}
Causes a compile-time error because it attempts to use it before assigning a value to the variable A.
A field is a variable that is associated with a class or struct or an instance of a class or struct. A field declared with the static modifier defines a static variable, and a field that is declared without this modifier defines the instance variable. A static field is associated with a type, whereas an instance variable is associated with an instance. Example
Using Personnel.data;
Class Employee
{
private static DataSet DS;
public string Name;
public decimal Salary;
...
}
Shows the Employee class with a private static variable and two public instance variables.
A parameter declaration also defines a variable. There are four types of parameters: value parameters, reference parameters, output parameters, and parameter arrays.
The value parameter is used for "in" parameter passing. In this procedure, the value of the argument is passed into the method. Because a value parameter is stored in a method with its own variable (rather than the original argument), modifications to this parameter do not affect the original argument. A variable of a value parameter is initialized by copying the value of the original argument. Example
Using System;
Class Test {
static void F (int p) {
Console.WriteLine ("P = {0}", p);
p++;
}
static void Main () {
int a = 1;
Console.WriteLine ("pre:a = {0}", a);
F (a);
Console.WriteLine ("post:a = {0}", a);
}
}
Shows a method F with a value parameter named P. The sample output results are as follows
PRE:A = 1
p = 1
POST:A = 1
Even though the value parameter P has been modified.
Reference parameters are passed by the "by reference" parameter. In this procedure, reference parameter is the alias of the argument supplied by the caller. Reference parameters do not define their own variables, but refer directly to the original argument, so modifications to reference parameters directly affect the value of the corresponding original argument. Reference parameters are declared with a ref modifier. Example
Using System;
Class Test {
static void Swap (ref int A, ref int b) {
int t = A;
A = b;
b = t;
}
static void Main () {
int x = 1;
int y = 2;
Console.WriteLine ("pre:x = {0}, y = {1}", X, y);
Swap (ref x, ref y);
Console.WriteLine ("post:x = {0}, y = {1}", X, y);
}
}
Shows a Swap method with two reference parameters. The resulting output is:
pre:x = 1, y = 2
Post:x = 2, y = 1
The REF keyword must be used in both formal parameter declarations and in use of formal parameters. Using ref at the call location can cause special attention to parameters, so that the developer who reads the code knows that the parameter value can be changed by the call.
For output parameters, the initial value of the argument supplied by the caller is not important, except that the output parameter is similar to the reference parameter. Output parameters are declared with an out modifier. Example
Using System;
Class Test {
static void Divide (int a, int b, out int result, out int remainder) {
result = A/b;
remainder = a% B;
}
static void Main () {
for (int i = 1; i < i++)
for (int j = 1; J < J + +) {
int ans, R;
Divide (I, J, out ans, out R);
Console.WriteLine ("{0}/{1} = {2}r{3}", I, J, ans, R);
}
}
}
Shows a Divide method that contains two output parameters: one for the result of the division and another for the remainder.
For parameters of the three types of values, references, and outputs, there is a one-to-one correspondence between the arguments provided by the caller and the parameters used to represent them. An array of arguments allows for a many-to-many relationship: Multiple arguments can be represented by an independent variable. In other words, a parameter array allows a variable-length list of independent variables.
The parameter array is declared with the params modifier. A given method can have only one parameter array, and it must always be the last specified parameter. The type of the parameter array is always a one-dimensional array type. The caller can pass an array variable of the same type, or any number of independent variables of the same type as the elements of the array. For example, an example
Using System;
Class Test
{
static void F (params int[] args) {
Console.WriteLine ("# of arguments: {0}", args.) Length);
for (int i = 0; i < args. Length; i++)
Console.WriteLine ("\targs[{0}] = {1}", I, args[i]);
}
static void Main () {
F ();
F (1);
F (1, 2);
F (1, 2, 3);
F (New int[] {1, 2, 3, 4});
}
}
Shows the method F with a variable int parameter, and several calls to this method. The output is:
# of arguments:0
# of Arguments:1
Args[0] = 1
# of Arguments:2
Args[0] = 1
ARGS[1] = 2
# of Arguments:3
Args[0] = 1
ARGS[1] = 2
ARGS[2] = 3
# of Arguments:4
Args[0] = 1
ARGS[1] = 2
ARGS[2] = 3
ARGS[3] = 4
Most of the examples that appear in this introduction use the WriteLine method of the Console class. The parameter substitution behavior of this method (as shown in the following example)
int a = 1, b = 2;
Console.WriteLine ("A = {0}, B = {1}", A, b);
is done using an array of parameters. To meet the common needs, the WriteLine method has several overloaded methods, some of which need to pass a fixed number of parameters, some of which use a parameter array.
Namespace System
{
public class Console
{
public static void WriteLine (string s) {...}
public static void WriteLine (string s, object a) {...}
public static void WriteLine (string s, Object A, object B) {...}
...
public static void WriteLine (string s, params object[] args) {...}
}
}