Advertisement
Felanpro

static variables

Aug 10th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace csharp_youtube_tutorial
  8. {
  9.     class myClass
  10.     {
  11.         public static int count = 0; //A static variable's value is shared among all instances of that class.
  12.     }
  13.  
  14.     class Program /*An instance of a class is another name for object*/
  15.     {
  16.         public static void Main(string[] args)
  17.         {
  18.             myClass obj1 = new myClass();
  19.  
  20.             myClass.count += 1; //You can directly acces the static variable from the class.
  21.  
  22.             Console.WriteLine(myClass.count);
  23.  
  24.             myClass obj2 = new myClass();
  25.  
  26.             Console.WriteLine(myClass.count); //This would have printed 1 if we had not set static to count.
  27.  
  28.             Console.ReadKey(); //Press a key application ends. //Console[Class] ReadKey()[Function/Method]  
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement