Static Members of a C# Class - CSharp Custom Type

CSharp examples for Custom Type:static

Description

Static Members of a C# Class

Demo Code

using System;//from w w w  .jav  a 2 s .  c o m
class StaticVar {
   public static int num;
   public void count() {
      num++;
   }
   public int getNum() {
      return num;
   }
}
class MainClass {
   static void Main(string[] args) {
      StaticVar s1 = new StaticVar();
      StaticVar s2 = new StaticVar();
      s1.count();
      s1.count();
      s1.count();
      s2.count();
      s2.count();
      s2.count();
      Console.WriteLine("Variable num for s1: {0}", s1.getNum());
      Console.WriteLine("Variable num for s2: {0}", s2.getNum());
   }
}

Result


Related Tutorials