<?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%2FC_Sharp%2FClass_Interface%2FStatic</id>
		<title>Csharp/C Sharp/Class Interface/Static - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FClass_Interface%2FStatic"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Class_Interface/Static&amp;action=history"/>
		<updated>2026-04-29T19:00:22Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/Class_Interface/Static&amp;diff=554&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Class_Interface/Static&amp;diff=554&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:18Z</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/C_Sharp/Class_Interface/Static&amp;diff=555&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Class_Interface/Static&amp;diff=555&amp;oldid=prev"/>
				<updated>2010-05-26T11:38:53Z</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;==Can call a non-static method through an       object reference from within a static method==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C#: The Complete Reference &lt;br /&gt;
by Herbert Schildt &lt;br /&gt;
Publisher: Osborne/McGraw-Hill (March 8, 2002)&lt;br /&gt;
ISBN: 0072134852&lt;br /&gt;
*/&lt;br /&gt;
using System;&lt;br /&gt;
public class MyClass { &lt;br /&gt;
  // non-static method. &lt;br /&gt;
  void nonStaticMeth() { &lt;br /&gt;
     Console.WriteLine(&amp;quot;Inside nonStaticMeth().&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  /* Can call a non-static method through an &lt;br /&gt;
     object reference from within a static method. */ &lt;br /&gt;
  public static void staticMeth(MyClass ob) { &lt;br /&gt;
    ob.nonStaticMeth(); // this is OK &lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Demonstrates access to static and non-static members==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C# Programming Tips &amp;amp; Techniques&lt;br /&gt;
by Charles Wright, Kris Jamsa&lt;br /&gt;
Publisher: Osborne/McGraw-Hill (December 28, 2001)&lt;br /&gt;
ISBN: 0072193794&lt;br /&gt;
*/&lt;br /&gt;
//&lt;br /&gt;
//  Members.cs -- Demonstrates access to static and non-static members&lt;br /&gt;
//&lt;br /&gt;
//                Compile this program using the following command line:&lt;br /&gt;
//                    C:&amp;gt;csc Members.cs&lt;br /&gt;
//&lt;br /&gt;
namespace nsMembers&lt;br /&gt;
{&lt;br /&gt;
    using System;&lt;br /&gt;
    &lt;br /&gt;
    public class StaticMembers&lt;br /&gt;
    {&lt;br /&gt;
        static public void Main ()&lt;br /&gt;
        {&lt;br /&gt;
            // Access a static member using the class name. &lt;br /&gt;
            // You may access a static&lt;br /&gt;
            // member without creating an instance of the class&lt;br /&gt;
            Console.WriteLine (&amp;quot;The static member is pi: &amp;quot; + clsClass.pi);&lt;br /&gt;
        &lt;br /&gt;
            // To access a non-static member, you must create an instance &lt;br /&gt;
            // of the class&lt;br /&gt;
            clsClass instance = new clsClass();&lt;br /&gt;
            // Access a static member using the name of the variable &lt;br /&gt;
            // containing the&lt;br /&gt;
            // instance reference&lt;br /&gt;
            Console.WriteLine (&amp;quot;The instance member is e: &amp;quot; + instance.e);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    class clsClass&lt;br /&gt;
    {&lt;br /&gt;
        // Declare a static field. You also could use the const &lt;br /&gt;
        // keyword instead of static&lt;br /&gt;
        static public double pi = 3.14159;&lt;br /&gt;
        // Declare a normal member, which will be created when you &lt;br /&gt;
        // declare an instance&lt;br /&gt;
        // of the class&lt;br /&gt;
        public double e = 2.71828;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Demonstrates how a static field is shared by multiple instances of a class==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C# Programming Tips &amp;amp; Techniques&lt;br /&gt;
by Charles Wright, Kris Jamsa&lt;br /&gt;
Publisher: Osborne/McGraw-Hill (December 28, 2001)&lt;br /&gt;
ISBN: 0072193794&lt;br /&gt;
*/&lt;br /&gt;
//&lt;br /&gt;
// Static.cs -- Demonstrates how a static field is shared by&lt;br /&gt;
//              multiple instances of a class.&lt;br /&gt;
//&lt;br /&gt;
//              Compile this program with the following command line:&lt;br /&gt;
//                  C:&amp;gt;csc Static.cs&lt;br /&gt;
//&lt;br /&gt;
namespace nsStatic&lt;br /&gt;
{&lt;br /&gt;
    using System;&lt;br /&gt;
    &lt;br /&gt;
    public class clsMainStatic&lt;br /&gt;
    {&lt;br /&gt;
        static public void Main ()&lt;br /&gt;
        {&lt;br /&gt;
            for (int i = 0; i &amp;lt; 20; ++i)&lt;br /&gt;
            {&lt;br /&gt;
                clsStatic inst = new clsStatic();&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine (&amp;quot;Created {0} instance of clsStatic&amp;quot;,&lt;br /&gt;
                               clsStatic.Count);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    class clsStatic&lt;br /&gt;
    {&lt;br /&gt;
        static public int Count&lt;br /&gt;
        {&lt;br /&gt;
            get {return (m_Count);}&lt;br /&gt;
        }&lt;br /&gt;
        static private int m_Count = 0;&lt;br /&gt;
        public clsStatic ()&lt;br /&gt;
        {&lt;br /&gt;
            ++m_Count;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Demonstrates use of static constructor==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C# Programming Tips &amp;amp; Techniques&lt;br /&gt;
by Charles Wright, Kris Jamsa&lt;br /&gt;
Publisher: Osborne/McGraw-Hill (December 28, 2001)&lt;br /&gt;
ISBN: 0072193794&lt;br /&gt;
*/&lt;br /&gt;
// SysInfo.cs -- Demonstrates use of static constructor&lt;br /&gt;
//&lt;br /&gt;
//               Compile this program with the following command line:&lt;br /&gt;
//                   C:&amp;gt;csc SysInfo.cs&lt;br /&gt;
//&lt;br /&gt;
namespace nsSysInfo&lt;br /&gt;
{&lt;br /&gt;
    using System;&lt;br /&gt;
    using System.Runtime.InteropServices;&lt;br /&gt;
    using System.Windows.Forms;&lt;br /&gt;
    &lt;br /&gt;
    public class SysInfo&lt;br /&gt;
    {&lt;br /&gt;
        static public void Main()&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine (&amp;quot;Current user is &amp;quot; +&lt;br /&gt;
                               clsSystemInfo.User);&lt;br /&gt;
            Console.WriteLine (&amp;quot;Current Time Zone is &amp;quot; +&lt;br /&gt;
                               clsSystemInfo.TZ);&lt;br /&gt;
            Console.WriteLine (&amp;quot;Current domain is &amp;quot; +&lt;br /&gt;
                               clsSystemInfo.Domain);&lt;br /&gt;
            Console.WriteLine (&amp;quot;Current Host is &amp;quot; +&lt;br /&gt;
                               clsSystemInfo.Host);&lt;br /&gt;
            Console.WriteLine (&amp;quot;Command interpreter is &amp;quot; + &lt;br /&gt;
                               clsSystemInfo.ruSpec);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    class clsSystemInfo&lt;br /&gt;
    {&lt;br /&gt;
        private clsSystemInfo () {}&lt;br /&gt;
        [DllImport (&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern public long GetEnvironmentVariable (string name,&lt;br /&gt;
                                            byte [] value, long size);&lt;br /&gt;
        static clsSystemInfo ()&lt;br /&gt;
        {&lt;br /&gt;
            m_User = SystemInformation.UserName;&lt;br /&gt;
            m_Host = SystemInformation.ruputerName;&lt;br /&gt;
            DateTime now = DateTime.Now;&lt;br /&gt;
            TimeZone tz = TimeZone.CurrentTimeZone;&lt;br /&gt;
            m_TimeZone = tz.IsDaylightSavingTime(now)&lt;br /&gt;
                         ? tz.DaylightName : tz.StandardName;&lt;br /&gt;
            m_Domain = SystemInformation.UserDomainName;&lt;br /&gt;
            byte [] comspec = new byte [256];&lt;br /&gt;
            if (GetEnvironmentVariable (&amp;quot;COMSPEC&amp;quot;, comspec, 256) &amp;gt; 0)&lt;br /&gt;
            {&lt;br /&gt;
                foreach (byte ch in comspec)&lt;br /&gt;
                {&lt;br /&gt;
                    if (ch == 0)&lt;br /&gt;
                        break;&lt;br /&gt;
                    m_ComSpec += (char) ch;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static public string User&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return (m_User);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static public string TZ&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return (m_TimeZone);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static public string Domain&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return (m_Domain);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static public string Host&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return (m_Host);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static public string ComSpec&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return (m_ComSpec);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static string m_User;&lt;br /&gt;
        private static string m_TimeZone;&lt;br /&gt;
        private static string m_Domain;&lt;br /&gt;
        private static string m_Host;&lt;br /&gt;
        private static string m_ComSpec;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Error using static==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C#: The Complete Reference &lt;br /&gt;
by Herbert Schildt &lt;br /&gt;
Publisher: Osborne/McGraw-Hill (March 8, 2002)&lt;br /&gt;
ISBN: 0072134852&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
using System; &lt;br /&gt;
 &lt;br /&gt;
public class AnotherStaticError { &lt;br /&gt;
  // non-static method. &lt;br /&gt;
  void nonStaticMeth() { &lt;br /&gt;
     Console.WriteLine(&amp;quot;Inside nonStaticMeth().&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  /* Error! Can&amp;quot;t directly call a non-static method &lt;br /&gt;
     from within a static method. */ &lt;br /&gt;
  static void staticMeth() { &lt;br /&gt;
    nonStaticMeth(); // won&amp;quot;t compile &lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Illustrates the use of static members==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Mastering Visual C# .NET&lt;br /&gt;
by Jason Price, Mike Gunderloy&lt;br /&gt;
Publisher: Sybex;&lt;br /&gt;
ISBN: 0782129110&lt;br /&gt;
*/&lt;br /&gt;
/*&lt;br /&gt;
  Example6_1.cs illustrates the use of static members&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// declare the Car class&lt;br /&gt;
class Car&lt;br /&gt;
{&lt;br /&gt;
  // declare a static field,&lt;br /&gt;
  // numberOfCars stores the number of Car objects&lt;br /&gt;
  private static int numberOfCars = 0;&lt;br /&gt;
  // define the constructor&lt;br /&gt;
  public Car()&lt;br /&gt;
  {&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;Creating a Car object&amp;quot;);&lt;br /&gt;
    numberOfCars++;  // increment numberOfCars&lt;br /&gt;
  }&lt;br /&gt;
  // define the destructor&lt;br /&gt;
  ~Car()&lt;br /&gt;
  {&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;Destroying a Car object&amp;quot;);&lt;br /&gt;
    numberOfCars--;  // decrement numberOfCars&lt;br /&gt;
  }&lt;br /&gt;
  // define a static method that returns numberOfCars&lt;br /&gt;
  public static int GetNumberOfCars()&lt;br /&gt;
  {&lt;br /&gt;
    return numberOfCars;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Example6_1&lt;br /&gt;
{&lt;br /&gt;
  public static void Main()&lt;br /&gt;
  {&lt;br /&gt;
    // display numberOfCars&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;Car.GetNumberOfCars() = &amp;quot; +&lt;br /&gt;
      Car.GetNumberOfCars());&lt;br /&gt;
    // create a Car object&lt;br /&gt;
    Car myCar = new Car();&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;Car.GetNumberOfCars() = &amp;quot; +&lt;br /&gt;
      Car.GetNumberOfCars());&lt;br /&gt;
    // create another Car object&lt;br /&gt;
    Car myCar2 = new Car();&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;Car.GetNumberOfCars() = &amp;quot; +&lt;br /&gt;
      Car.GetNumberOfCars());&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Static members are frequently used as counters.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
public class Starter {&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        MyClass&amp;lt;int&amp;gt; obj1 = new MyClass&amp;lt;int&amp;gt;();&lt;br /&gt;
        MyClass&amp;lt;double&amp;gt; obj2 = new MyClass&amp;lt;double&amp;gt;();&lt;br /&gt;
        MyClass&amp;lt;double&amp;gt; obj3 = new MyClass&amp;lt;double&amp;gt;();&lt;br /&gt;
        MyClass&amp;lt;int&amp;gt;.Count(obj1);&lt;br /&gt;
        MyClass&amp;lt;double&amp;gt;.Count(obj2);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class MyClass&amp;lt;T&amp;gt; {&lt;br /&gt;
    public MyClass() {&lt;br /&gt;
        ++counter;&lt;br /&gt;
    }&lt;br /&gt;
    public static void Count(MyClass&amp;lt;T&amp;gt; _this) {&lt;br /&gt;
        Console.WriteLine(&amp;quot;{0} : {1}&amp;quot;,&lt;br /&gt;
            _this.GetType().ToString(),&lt;br /&gt;
            counter.ToString());&lt;br /&gt;
    }&lt;br /&gt;
    private static int counter = 0;&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use a static class factory==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C#: The Complete Reference &lt;br /&gt;
by Herbert Schildt &lt;br /&gt;
Publisher: Osborne/McGraw-Hill (March 8, 2002)&lt;br /&gt;
ISBN: 0072134852&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// Use a static class factory. &lt;br /&gt;
 &lt;br /&gt;
using System; &lt;br /&gt;
 &lt;br /&gt;
class MyClass { &lt;br /&gt;
  int a, b; &lt;br /&gt;
 &lt;br /&gt;
  // Create a class factory for MyClass. &lt;br /&gt;
  static public MyClass factory(int i, int j) { &lt;br /&gt;
    MyClass t = new MyClass(); &lt;br /&gt;
    &lt;br /&gt;
    t.a = i; &lt;br /&gt;
    t.b = j; &lt;br /&gt;
 &lt;br /&gt;
    return t; // return an object &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  public void show() { &lt;br /&gt;
    Console.WriteLine(&amp;quot;a and b: &amp;quot; + a + &amp;quot; &amp;quot; + b); &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
} &lt;br /&gt;
  &lt;br /&gt;
public class MakeObjects1 { &lt;br /&gt;
  public static void Main() {   &lt;br /&gt;
    int i, j; &lt;br /&gt;
 &lt;br /&gt;
    // generate objects using the factory &lt;br /&gt;
    for(i=0, j=10; i &amp;lt; 10; i++, j--) { &lt;br /&gt;
      MyClass ob = MyClass.factory(i, j); // get an object &lt;br /&gt;
      ob.show(); &lt;br /&gt;
    } &lt;br /&gt;
       &lt;br /&gt;
    Console.WriteLine();    &lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use a static constructor==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C#: The Complete Reference &lt;br /&gt;
by Herbert Schildt &lt;br /&gt;
Publisher: Osborne/McGraw-Hill (March 8, 2002)&lt;br /&gt;
ISBN: 0072134852&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// Use a static constructor. &lt;br /&gt;
 &lt;br /&gt;
using System; &lt;br /&gt;
 &lt;br /&gt;
class Cons { &lt;br /&gt;
  public static int alpha; &lt;br /&gt;
  public int beta; &lt;br /&gt;
 &lt;br /&gt;
  // static constructor &lt;br /&gt;
  static Cons() { &lt;br /&gt;
    alpha = 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;
    beta = 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;
public class ConsDemo { &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.alpha: &amp;quot; + Cons.alpha); &lt;br /&gt;
    Console.WriteLine(&amp;quot;ob.beta: &amp;quot; + ob.beta); &lt;br /&gt;
                  &lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&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;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C#: The Complete Reference &lt;br /&gt;
by Herbert Schildt &lt;br /&gt;
Publisher: Osborne/McGraw-Hill (March 8, 2002)&lt;br /&gt;
ISBN: 0072134852&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// Use a static field to count instances. &lt;br /&gt;
 &lt;br /&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;
public class CountDemo {  &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;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use static==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C#: The Complete Reference &lt;br /&gt;
by Herbert Schildt &lt;br /&gt;
Publisher: Osborne/McGraw-Hill (March 8, 2002)&lt;br /&gt;
ISBN: 0072134852&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// Use static. &lt;br /&gt;
 &lt;br /&gt;
using System; &lt;br /&gt;
 &lt;br /&gt;
class StaticDemo { &lt;br /&gt;
  // a static variable &lt;br /&gt;
  public static int val = 100;  &lt;br /&gt;
 &lt;br /&gt;
  // a static method &lt;br /&gt;
  public static int valDiv2() { &lt;br /&gt;
    return val/2; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
public class SDemo { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;Initial value of StaticDemo.val is &amp;quot; &lt;br /&gt;
                      + StaticDemo.val); &lt;br /&gt;
 &lt;br /&gt;
    StaticDemo.val = 8; &lt;br /&gt;
    Console.WriteLine(&amp;quot;StaticDemo.val is &amp;quot; + StaticDemo.val); &lt;br /&gt;
    Console.WriteLine(&amp;quot;StaticDemo.valDiv2(): &amp;quot; + &lt;br /&gt;
                       StaticDemo.valDiv2()); &lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use static method to initialize field==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
internal class MyClass {&lt;br /&gt;
    public int iField1 = FuncA();&lt;br /&gt;
    public int iField2 = FuncC();&lt;br /&gt;
    public int iField3 = FuncB();&lt;br /&gt;
    public static int FuncA() {&lt;br /&gt;
        Console.WriteLine(&amp;quot;MyClass.FuncA&amp;quot;);&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public static int FuncB() {&lt;br /&gt;
        Console.WriteLine(&amp;quot;MyClass.FuncB&amp;quot;);&lt;br /&gt;
        return 1;&lt;br /&gt;
    }&lt;br /&gt;
    public static int FuncC() {&lt;br /&gt;
        Console.WriteLine(&amp;quot;MyClass.FuncC&amp;quot;);&lt;br /&gt;
        return 2;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class Starter {&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        MyClass obj = new MyClass();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>