Tuesday, December 25, 2007

Static Members (1) A Test Program


using System;

namespace Console_CSharp

{

class TestStatic

{

static void Main(string[] args)

{

//----------Test_StaticField-----------

Console.WriteLine("{0},{1},{2}", Test_StaticField.x, Test_StaticField.y, Test_StaticField.z); //1,2,0

Test_StaticField t = new Test_StaticField(3);

Console.WriteLine("{0},{1},{2}", Test_StaticField.x, Test_StaticField.y, Test_StaticField.z); //3,3,3

//----------Test_StaticConstructor-----------

Console.WriteLine("{0},{1}", Test_StaticConstructor.x, Test_StaticConstructor.y); //100,200

//----------Test_StaticMethod-----------

Test_StaticMethod.Method(); //10,20

//----------Test_StaticProperty-----------

Test_StaticProperty.X = 20; //SET

int val = Test_StaticProperty.X; //GET

//----------TestInheritance-----------

Console.WriteLine(SubTest_Inheritance.x); //25

//----------Test_InheritanceHide-----------

SubTest_InheritanceHide.Method(); // Derived static method

Console.WriteLine(SubTest_InheritanceHide.x); //50

Console.ReadLine();

}

}

}

//--------------------------------------------------------------------------------------------------

class Test_StaticField

{

public static int x = 1;

public static int y = 2;

public static int z; //default value = 0 before initialization

public Test_StaticField(int i)

{

x = i; //normal constructor can access static members

y = i;

z = i;

}

}

//--------------------------------------------------------------------------------------------------

class Test_StaticConstructor

{

public static int x;

public static int y;

public int z; //default value = 0 before initialization

static Test_StaticConstructor() //static constructor is to initialize the static members

{

x = 100;

y = 200;

//z = 300; //compile error - static constructors can't access non-static data members

}

}

//--------------------------------------------------------------------------------------------------

class Test_StaticMethod

{

private static int x = 10;

private static int y = 20;

private int z = 30;

public static void Method()

{

Console.WriteLine("{0},{1}", x, y); //cannot access z here, static method can access only static members

}

}

//--------------------------------------------------------------------------------------------------

class Test_StaticProperty

{

public static int X

{

get

{

Console.WriteLine("GET");

return 10;

}

set

{

Console.WriteLine("SET");

}

}

}

//--------------------------------------------------------------------------------------------------

class Test_Inheritance

{

public static int x = 25;

}

class SubTest_Inheritance : Test_Inheritance

{

}

//--------------------------------------------------------------------------------------------------

class Test_InheritanceHide

{

public static int x = 25;

public static void Method()

{

Console.WriteLine("Base static method");

}

}

class SubTest_InheritanceHide : Test_InheritanceHide

{

public new static int x = 50;

//A static member can't be marked as override, virtual or abstract.

//However it is possible to hide a base class static method in a derived class by using the keyword new.

public new static void Method()

{

Console.WriteLine("Derived static method");

}

}

C# - Static Members

blog comments powered by Disqus