C# static Keyword

In C#, if we use a static keyword with class members, then there will be a single copy of the type member.

And all objects of the class share a single copy instead of creating individual copies.

C# Static Variables

If a variable is declared static, we can access the variable using the class name. For example,

using System;

class Program
{
    static void Main(string[] argos)
    {
        // access static variable
        Console.WriteLine("Department: " + Student.department);
        Console.ReadLine();
    }
}

class Student
{
    // static variable
    public static string department = "Computer Science";
}

In the above example, we have created a static variable named department. Since the variable is static, we have used the class name Student to access the variable.

Example: C# Static Variable Vs. Instance Variable

using System;
class Student
{
    static public string schoolName = "Coding Room";
    public string studentName;

}
class Program
{
    static void Main(string[] args)
    {
        Student s1 = new Student();
        s1.studentName = "Ram";

        Console.WriteLine(s1.studentName);

        Student s2 = new Student();
        s2.studentName = "Shyam";

        Console.WriteLine(s2.studentName);

        Console.WriteLine(Student.schoolName);

        Console.ReadLine();
    }
}

In the above program, the Student class has a non-static variable named studentName and a static variable named schoolName.

Inside the Program class,

  • s1.studentName / s2.studentName - calls the non-static variable using objects s1 and s2 respectively

  • Student.schoolName - calls the static variable by using the class name Since the schoolName is the same for all students, it is good to make the schoolName static. It saves memory and makes the program more efficient.

C# Static Methods

Just like static variables, we can call the static methods using the class name.

using System;
class Student
{
    public string name;
    static int count;
    public Student() => count++;
    public static int GetCount() => count;
}
class Program
{
    static void Main(string[] args)
    {
        Student s1 = new Student();
        s1.name = "Ram";

        Student s2 = new Student();
        s2.name = "Tom";

        Console.WriteLine("Student Count: " + Student.GetCount());

        Console.ReadLine();
    }
}

Here, we have accessed the static method directly from Program classes using the class name.

When we declare a method static, all objects of the class share the same static method.

Note: In C#, the Main method is static. So, we can call it without creating the object!

Static methods and instance variable

Static methods cannot make use of instance members (non-static). If you think about it, it is very natural for this restriction. Instance members belong to a specific object, where as static methods are defined on the whole class, without any intention to manipulate variable of any specific object.

using System;
class Student
{
    public string name;
    static int count;
    public Student() => count++;
    public static int GetCount() => count;

    public static void Display()
    {
        Console.WriteLine(" ... Student Info ....");
        //wrong, error!
        Console.WriteLine(name);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Student s1 = new Student();
        s1.name = "Ram";

        Student s2 = new Student();
        s2.name = "Tom";

        Console.WriteLine("Student Count: " + Student.GetCount());

        Student.Display();

        Console.ReadLine();
    }
}

Static methods cannot make use of instance members, whereas normal methods (non-static methods) can access any type of members (static or non-static)!

Last updated