Static Keyword in C#

Static Keyword

Static keyword can be used in 3 scenarios,

1. For Data Members

It is a variable which belongs to the Class. A single copy can be shared by all instances of a class. For the used of static variables, creation of instance is not required.

Access using ClassName.VariableName

Unlike instance variables which are accessed as ObjectName.VariableName.
 
Static Data Member - > StaticDataMember.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo.Classes

{

    public class StaticDataMember

    {

        private int StudId;

        private static int nextStudId = 1;

 

        public StaticDataMember()

        {

            StudId = nextStudId++;

        }

 

        public void PrintRecord()

        {

            Console.WriteLine("Student Id is {0}", StudId);

        }

    }

}

 

using StaticKeyWordDemo.Classes;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo

{

    class StaticKeyword

    {       

        static void Main(string[] args)

        {

            StaticDataMember sd1 = new StaticDataMember();

            StaticDataMember sd2 = new StaticDataMember();

 

            //printing

            Console.WriteLine("*** Static Keyword Demo ***");

            sd1.PrintRecord();

            sd2.PrintRecord();

            Console.ReadKey();

        }

    }

}
 
Output

Image-1.jpg

2. tatic Method Member

Like any static member, static method belongs to the class(not object). Accessed using ClassName.MethodName.It is a class method. For using static methods, Creation of instance is not necessary. A static method only access other static data and methods, Static methods can not access data members and members method directly – they must use an object reference.
 
Static Data Members - > StaticMethodMember.cs
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo.Classes

{

    public class StaticMethodMember

    {

        private int EmpId;

        private static int nextEmpId = 100;

 

        public static void PrintNextEmp()

        {

            Console.WriteLine("Next Employee Id is {0}", nextEmpId);

        }

    }

}

 

using StaticKeyWordDemo.Classes;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo

{

    class StaticKeyword

    {       

        static void Main(string[] args)

        {

            Console.WriteLine("*** Static Keyword Demo ***");

           

            StaticMethodMember.PrintNextEmp();

 

            Console.ReadKey();

        }

    }

}
 
Output

Image-2.jpg

3. Static Constructor

A class can have only one static member. Static constructor can not have any parameter. Static constructor can not have any access specifier, not even private. It is used to initialize the static data members of the class. For any number of object creation, the static constructor gets executed only once.

The static constructor gets excuted when the class is used or refernced for the very first time in the applocation. It can not be invoked by the programmer explicitly.
           
Static Constructor - > StaticConstructor.cs
 
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo.Classes

{

    public class StaticConstructor

    {

        private static int nextEmpId;

        private int EmpId;

        private string EmpName = "Krishn";

 

        static StaticConstructor()

        {

            nextEmpId = 100;

        }

 

        public StaticConstructor()

        {

            EmpId = nextEmpId++;

        }

 

        public StaticConstructor(string empName)

        {

            EmpId = nextEmpId++;

            this.EmpName = empName;

        }

 

        public void PrintInfo()

        {

            Console.WriteLine("EmpId : {0}", EmpId);

            Console.WriteLine("EmpName : {0}", EmpName);

        }

    }

}

 

using StaticKeyWordDemo.Classes;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo

{

    class StaticKeyword

    {       

        static void Main(string[] args)

        {

            Console.WriteLine("*** Static Keyword Demo ***");

           

            StaticConstructor sc1 = new StaticConstructor();

            sc1.PrintInfo();

            Console.WriteLine();

            StaticConstructor sc2 = new StaticConstructor("Jeetendra");

            sc2.PrintInfo();

 

            Console.ReadKey();

        }

    }

}

  
Output

Image-3.jpg
 
The first time during class load for execution, the nexEmpId gets initialized to 100. after that every instance of StaticConstructor class created using either of the constructors, the nextEmpId is assigned to the instance field empId and then incremented. Thus this code generates a unique number stating from 100 and assigns it to each individual StaticConstructor object as its empId.

Static constructor is called implicitly only once in the lifetime of the application. Subsequently only the default or parameterised constructor is called, depending on the way of object created.

Ebook Download
View all
Learn
View all