In this program, we build our own type called Counter and another type called Test that uses instances of the Counter. The Counter type uses the predefined type int, and the Test type uses the static function member WriteLine of the Console class defined in the System namespace:
// Imports types from System namespace, such as Console
using System;
class Counter { // New types are typically classes or structs
// --- Data members ---
int value; // field of type int
int scaleFactor; // field of type int
// Constructor, used to initialize a type instance
Variables in C# (except in unsafe contexts) must be assigned a value before they are used. A variable is either explicitly assigned a value or automatically assigned a default value. Automatic assignment occurs for static fields, class instance fields, and array elements not explicitly assigned a value. For example:
using System;
class Test {
int v;
// Constructors that initialize an instance of a Test
public Test( ) {} // v will be automatically assigned to 0
public Test(int a) { // explicitly assign v a value
v = a;
}
static void Main( ) {
Test[] iarr = new Test [2]; // declare array
Console.WriteLine(iarr[1]); // ok, elements assigned to null