An array that is not initialized with values for its entries take on different values, depending on the type of variable allocated in the array. Variables come in two basic types: value and reference types. Value types are automatically allocated and initialized with their default value: zero for integers. References types must have their allocation specified explicitly; otherwise, they are simmply "null." An example of each type of array is shown below.
using System;
namespace XoaX {
class Program {
static void Main(string[] args) {
// Value type array with default value = 0
int[] saValueType = new int[3];
Console.WriteLine("Value Type:");
Console.WriteLine(saValueType[0]);
Console.WriteLine(Convert.ToString(saValueType[1]));
Console.WriteLine(saValueType[2].ToString());
Console.WriteLine();
// Reference type array with null refernces
string[] asReferenceType = new string[3];
Console.WriteLine("Reference Type:");
Console.WriteLine(asReferenceType[0]);
Console.WriteLine(Convert.ToString(asReferenceType[1]));
//Console.WriteLine(asReferenceType[2].ToString()); // This throws an exception!
Console.WriteLine();
}
}
}
Value Type: 0 0 0 Reference Type: Press any key to continue . . .
© 20072025 XoaX.net LLC. All rights reserved.