<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FClass%2Fstatic_field</id>
		<title>Csharp/CSharp Tutorial/Class/static field - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FClass%2Fstatic_field"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/static_field&amp;action=history"/>
		<updated>2026-04-29T19:35:22Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/static_field&amp;diff=5641&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/static_field&amp;diff=5641&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/static_field&amp;diff=5642&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/static_field&amp;diff=5642&amp;oldid=prev"/>
				<updated>2010-05-26T12:16:05Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Instance Counting==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System.ruponentModel;&lt;br /&gt;
    public class MainClass&lt;br /&gt;
    {&lt;br /&gt;
        public string Name { get; private set; }&lt;br /&gt;
        public int Age { get; private set; }&lt;br /&gt;
        private static int InstanceCounter { get; set; }&lt;br /&gt;
        private static readonly object counterLock = new object();&lt;br /&gt;
        public MainClass(string name, int age)&lt;br /&gt;
        {&lt;br /&gt;
            Name = name;&lt;br /&gt;
            Age = age;&lt;br /&gt;
            lock (counterLock)&lt;br /&gt;
            {&lt;br /&gt;
                InstanceCounter++;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reference a static member variable without using the class name==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;public class Employee&lt;br /&gt;
{&lt;br /&gt;
   public Employee()&lt;br /&gt;
   {&lt;br /&gt;
      totalHeadCount = 123;&lt;br /&gt;
      Employee.totalHeadCount = 456;&lt;br /&gt;
   }&lt;br /&gt;
   public static int totalHeadCount;&lt;br /&gt;
}&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
   static void Main()&lt;br /&gt;
   {&lt;br /&gt;
      Employee bob = new Employee();&lt;br /&gt;
      &lt;br /&gt;
      System.Console.WriteLine(&amp;quot;Static Field Employee.totalHeadCount is {0}&amp;quot;,Employee.totalHeadCount );&lt;br /&gt;
   }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Static Field Employee.totalHeadCount is 456&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reference Static Field without Instance==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
class MyClass&lt;br /&gt;
{&lt;br /&gt;
   static public int myStaticValue;&lt;br /&gt;
}&lt;br /&gt;
class Program&lt;br /&gt;
{&lt;br /&gt;
   static void Main()&lt;br /&gt;
   {&lt;br /&gt;
      MyClass.myStaticValue = 5;&lt;br /&gt;
      Console.WriteLine(&amp;quot;myStaticValue = {0}&amp;quot;, MyClass.myStaticValue);&lt;br /&gt;
   }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;myStaticValue = 5&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Static field init==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
public class MyClass&lt;br /&gt;
{&lt;br /&gt;
   private static int InitX()&lt;br /&gt;
   {&lt;br /&gt;
      Console.WriteLine( &amp;quot;MyClass.InitX()&amp;quot; );&lt;br /&gt;
      return 1;&lt;br /&gt;
   }&lt;br /&gt;
   private static int InitY()&lt;br /&gt;
   {&lt;br /&gt;
      Console.WriteLine( &amp;quot;MyClass.InitY()&amp;quot; );&lt;br /&gt;
      return 2;&lt;br /&gt;
   }&lt;br /&gt;
   private static int InitA()&lt;br /&gt;
   {&lt;br /&gt;
      Console.WriteLine( &amp;quot;MyClass.InitA()&amp;quot; );&lt;br /&gt;
      return 3;&lt;br /&gt;
   }&lt;br /&gt;
   private static int InitB()&lt;br /&gt;
   {&lt;br /&gt;
      Console.WriteLine( &amp;quot;MyClass.InitB()&amp;quot; );&lt;br /&gt;
      return 4;&lt;br /&gt;
   }&lt;br /&gt;
   private int y = InitY();&lt;br /&gt;
   private int x = InitX();&lt;br /&gt;
   private static int a = InitA();&lt;br /&gt;
   private static int b = InitB();&lt;br /&gt;
}&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
   static void Main()&lt;br /&gt;
   {&lt;br /&gt;
      MyClass a = new MyClass();&lt;br /&gt;
   }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;MyClass.InitA()&lt;br /&gt;
MyClass.InitB()&lt;br /&gt;
MyClass.InitY()&lt;br /&gt;
MyClass.InitX()&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Static Fields==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;OL&amp;gt;&amp;lt;LI&amp;gt;A static member can be accessed before any objects of its class are created.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;You can call a static member without referencing to any object.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;You can declare both static methods and static variables.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;Variables declared as static are, essentially, global variables.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;All instances of the class share the same static variable.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;A static variable is initialized when its class is loaded.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;A static variable always has a value.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;If not initialized, a static variable is initialized to zero for numeric values.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;If not initialized, a static variable is initialized to null for object references.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;If not initialized, a static variable is initialized to false for variables of type bool.&amp;lt;/LI&amp;gt;&amp;lt;/OL&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
class MyClass&lt;br /&gt;
{&lt;br /&gt;
    public MyClass()&lt;br /&gt;
    {&lt;br /&gt;
        instanceCount++;&lt;br /&gt;
    }&lt;br /&gt;
    public static int instanceCount = 0;&lt;br /&gt;
}&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        MyClass my = new MyClass();&lt;br /&gt;
        Console.WriteLine(MyClass.instanceCount);&lt;br /&gt;
        MyClass my2 = new MyClass();&lt;br /&gt;
        Console.WriteLine(MyClass.instanceCount);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;1&lt;br /&gt;
2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use a static constructor.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class Cons { &lt;br /&gt;
  public static int a; &lt;br /&gt;
  public int b; &lt;br /&gt;
 &lt;br /&gt;
  // static constructor &lt;br /&gt;
  static Cons() { &lt;br /&gt;
    a = 99; &lt;br /&gt;
    Console.WriteLine(&amp;quot;Inside static constructor.&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  // instance constructor &lt;br /&gt;
  public Cons() { &lt;br /&gt;
    b = 100; &lt;br /&gt;
    Console.WriteLine(&amp;quot;Inside instance constructor.&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
  &lt;br /&gt;
class MainClass { &lt;br /&gt;
  public static void Main() {   &lt;br /&gt;
    Cons ob = new Cons(); &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;Cons.a: &amp;quot; + Cons.a); &lt;br /&gt;
    Console.WriteLine(&amp;quot;ob.b: &amp;quot; + ob.b); &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Inside static constructor.&lt;br /&gt;
Inside instance constructor.&lt;br /&gt;
Cons.a: 99&lt;br /&gt;
ob.b: 100&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use a static field to count instances.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class CountInst {  &lt;br /&gt;
  static int count = 0; &lt;br /&gt;
  &lt;br /&gt;
  // increment count when object is created &lt;br /&gt;
  public CountInst() {  &lt;br /&gt;
    count++; &lt;br /&gt;
  }  &lt;br /&gt;
 &lt;br /&gt;
  // decrement count when object is destroyed &lt;br /&gt;
  ~CountInst() { &lt;br /&gt;
    count--; &lt;br /&gt;
  } &lt;br /&gt;
  &lt;br /&gt;
  public static int getcount() { &lt;br /&gt;
    return count; &lt;br /&gt;
  } &lt;br /&gt;
}  &lt;br /&gt;
  &lt;br /&gt;
class MainClass {  &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    CountInst ob; &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
    for(int i=0; i &amp;lt; 10; i++) { &lt;br /&gt;
      ob = new CountInst(); &lt;br /&gt;
      Console.WriteLine(&amp;quot;Current count: &amp;quot; +  &lt;br /&gt;
                        CountInst.getcount()); &lt;br /&gt;
    } &lt;br /&gt;
 &lt;br /&gt;
  }  &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Current count: 1&lt;br /&gt;
Current count: 2&lt;br /&gt;
Current count: 3&lt;br /&gt;
Current count: 4&lt;br /&gt;
Current count: 5&lt;br /&gt;
Current count: 6&lt;br /&gt;
Current count: 7&lt;br /&gt;
Current count: 8&lt;br /&gt;
Current count: 9&lt;br /&gt;
Current count: 10&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>