static keyword in C#

CsharpProgrammingServer Side Programming

We can define class members as static using the static keyword. When we declare a member of a class as static, it means no matter how many objects of the class are created, there is only one copy of the static member.

The keyword static implies that only one instance of the member exists for a class. Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it.

The following is an example showing the usage of static variables −


 Live Demo

using System;

namespace StaticVarApplication {
   class StaticVar {
      public static int num;

      public void count() {

      public int getNum() {
         return num;

   class StaticTester {
      static void Main(string[] args) {
         StaticVar s1 = new StaticVar();
         StaticVar s2 = new StaticVar();



         Console.WriteLine("Variable num for s1: {0}", s1.getNum());
         Console.WriteLine("Variable num for s2: {0}", s2.getNum());


Variable num for s1: 6
Variable num for s2: 6
Published on 08-Aug-2018 16:44:17