Classes and structs in C#: Static variables and methods
Notes:
using System;

class C {
static int objects = 0;
public int x, y;

public C() {
x = 0; y = 0;
objects++;
}

public static int ObjectCounter {
get { return objects; }
}

public static void ResetObjectCounter() {
objects = 0;
}
}

class Test {

public static void Main() {
C a = new C();
C b = new C();
C c = new C();
Console.WriteLine(C.ObjectCounter + " objects of C were created");
C.ResetObjectCounter();
a = new C();
c = new C();
Console.WriteLine(C.ObjectCounter + " objects of C were created");
}
}
No votes yet
Related Links:Classes and structs in C#: ClassesClasses and structs in C#: Structs and propertiesClasses and structs in C#: Variable number of parametersClasses and structs in C#: Parameter passing modesClasses and structs in C#: Cross-reference list
© Copyright 2008. All Rights Reserved.