Csharp/CSharp Tutorial/Class/static field
Содержание
Instance Counting
<source lang="csharp">using System.ruponentModel;
public class MainClass { public string Name { get; private set; } public int Age { get; private set; } private static int InstanceCounter { get; set; } private static readonly object counterLock = new object(); public MainClass(string name, int age) { Name = name; Age = age; lock (counterLock) { InstanceCounter++; } } }</source>
Reference a static member variable without using the class name
<source lang="csharp">public class Employee {
public Employee() { totalHeadCount = 123; Employee.totalHeadCount = 456; } public static int totalHeadCount;
} public class MainClass {
static void Main() { Employee bob = new Employee(); System.Console.WriteLine("Static Field Employee.totalHeadCount is {0}",Employee.totalHeadCount ); }
}</source>
Static Field Employee.totalHeadCount is 456
Reference Static Field without Instance
<source lang="csharp">using System; class MyClass {
static public int myStaticValue;
} class Program {
static void Main() { MyClass.myStaticValue = 5; Console.WriteLine("myStaticValue = {0}", MyClass.myStaticValue); }
}</source>
myStaticValue = 5
Static field init
<source lang="csharp">using System; public class MyClass {
private static int InitX() { Console.WriteLine( "MyClass.InitX()" ); return 1; } private static int InitY() { Console.WriteLine( "MyClass.InitY()" ); return 2; } private static int InitA() { Console.WriteLine( "MyClass.InitA()" ); return 3; } private static int InitB() { Console.WriteLine( "MyClass.InitB()" ); return 4; } private int y = InitY(); private int x = InitX(); private static int a = InitA(); private static int b = InitB();
} public class MainClass {
static void Main() { MyClass a = new MyClass(); }
}</source>
MyClass.InitA() MyClass.InitB() MyClass.InitY() MyClass.InitX()
Static Fields
- A static member can be accessed before any objects of its class are created.
- You can call a static member without referencing to any object.
- You can declare both static methods and static variables.
- Variables declared as static are, essentially, global variables.
- All instances of the class share the same static variable.
- A static variable is initialized when its class is loaded.
- A static variable always has a value.
- If not initialized, a static variable is initialized to zero for numeric values.
- If not initialized, a static variable is initialized to null for object references.
- If not initialized, a static variable is initialized to false for variables of type bool.
<source lang="csharp">using System;
class MyClass
{
public MyClass() { instanceCount++; } public static int instanceCount = 0;
} class MainClass {
public static void Main() { MyClass my = new MyClass(); Console.WriteLine(MyClass.instanceCount); MyClass my2 = new MyClass(); Console.WriteLine(MyClass.instanceCount); }
}</source>
1 2
Use a static constructor.
<source lang="csharp">using System;
class Cons {
public static int a; public int b; // static constructor static Cons() { a = 99; Console.WriteLine("Inside static constructor."); } // instance constructor public Cons() { b = 100; Console.WriteLine("Inside instance constructor."); }
}
class MainClass {
public static void Main() { Cons ob = new Cons(); Console.WriteLine("Cons.a: " + Cons.a); Console.WriteLine("ob.b: " + ob.b); }
}</source>
Inside static constructor. Inside instance constructor. Cons.a: 99 ob.b: 100
Use a static field to count instances.
<source lang="csharp">using System;
class CountInst {
static int count = 0; // increment count when object is created public CountInst() { count++; } // decrement count when object is destroyed ~CountInst() { count--; } public static int getcount() { return count; }
}
class MainClass {
public static void Main() { CountInst ob; for(int i=0; i < 10; i++) { ob = new CountInst(); Console.WriteLine("Current count: " + CountInst.getcount()); } }
}</source>
Current count: 1 Current count: 2 Current count: 3 Current count: 4 Current count: 5 Current count: 6 Current count: 7 Current count: 8 Current count: 9 Current count: 10