DEV Community

Cover image for All about Static in C#
shariaretanvir
shariaretanvir

Posted on

All about Static in C#

What is static means?
In real world static means something which is stable or sort of fixed. Something which will act similarly in all aspects.

In C# static is a keyword which makes a member, property, method, constructor or class as a static which we will understand one by one.

Static class
When we put static keyword on a class it becomes static class. And we cannot instantiate a static class. As a result a static class cannot be inherited by another class.

Example:

public static class Calculator
    {
        public static double Result { get; set; }
        public static string Mode { get; set; } = "Normal";
        public static double Add(double num1 , double num2)
        {
            return num1+num2;
        }

        public static void SaveResult(double result)
        {
            Result = result;
        }
    }

Enter fullscreen mode Exit fullscreen mode

In calling function

double result = Calculator.Add(10,20);
            Calculator.SaveResult(result);
            Console.WriteLine("Latest Result {0} ",Calculator.Mode);

Enter fullscreen mode Exit fullscreen mode

As we can see to call a static class we do not need to instantiate an instance.
Important things to note

  1. Static classes cannot be instantiated.
  2. All the members of a static class must be static; otherwise the compiler will give an error.
  3. A static class cannot contain instance members and constructors.
  4. Static classes are sealed class and therefore, cannot be inherited.
  5. Static class members can be accessed using ClassName.MemberName.
  6. A static class remains in memory for the lifetime of the application domain in which your program resides.

Static members:
Properties and field having static keyword called static members. Static members can be use inside non static class also. Usually we can use static members when there is like constant or fixed value.
Example :

public class Employee
    {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string CompanyName { get; set; }
        public static string Nationality { get; set; } = "Bangladeshi";

        public void SaveEmployee(Employee employee)
        {
            string a = Employee.Nationality;
        }
    }

Enter fullscreen mode Exit fullscreen mode

In the calling function

Employee employee = new Employee
            {
                EmployeeID = 0,
                Name = "Akash",
                CompanyName = "abc"
            };
            employee.SaveEmployee(employee);

Enter fullscreen mode Exit fullscreen mode

In this example we define Nationality as static because this is common for all objects. So we don’t need to define it for all objects.

Static constructor:
Constructor having static identifier called static constructor. It will execute only for once. It execute when static method is executes or creating an instance of that object for the first time. After that it will not being called. We call use this feature to initiate some static value for the first time. A non-static class can contain one parameter less static constructor. Parameterized static constructors are not allowed.

Example :

public class Employee
    {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string CompanyName { get; set; }
        public static string Nationality { get; set; }

        static Employee()
        {
            Nationality = "Bangladeshi";
        }
        public void SaveEmployee(Employee employee)
        {
            string a = Employee.Nationality;
        }
    }

Enter fullscreen mode Exit fullscreen mode
static void Main(string[] args)
        {
            //double result = Calculator.Add(10,20);
            //Calculator.SaveResult(result);
            //Console.WriteLine("Latest Result {0} ", Calculator.Mode);
            Employee employee = new Employee
            {
                EmployeeID = 0,
                Name = "Akash",
                CompanyName = "abc"
            };
            employee.SaveEmployee(employee);
            Console.WriteLine("Saved ");
        }

Enter fullscreen mode Exit fullscreen mode

Here Nationality is set in static constructor as it is common for all objects. A non-static class can contain one parameter less static constructor. Parameterized static constructors are not allowed.

Static method:
Method using static keyword is called static method. Concept of static method is easy. Basically the purpose of using static method is to use this method without creating instance.

Example:

public class Employee
    {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string CompanyName { get; set; }
        public static string Nationality { get; set; }

        static Employee()
        {
            Nationality = "Bangladeshi";
        }
        public static void SaveEmployee(Employee employee)
        {
            string a = Employee.Nationality;
        }
    }
Enter fullscreen mode Exit fullscreen mode
static void Main(string[] args)
        {
            //double result = Calculator.Add(10,20);
            //Calculator.SaveResult(result);
            //Console.WriteLine("Latest Result {0} ", Calculator.Mode);
            Employee employee = new Employee
            {
                EmployeeID = 0,
                Name = "Akash",
                CompanyName = "abc"
            };
            Employee.SaveEmployee(employee);
            Console.WriteLine("Saved ");
        }

Enter fullscreen mode Exit fullscreen mode

Static methods cannot access or call non-static variables unless they are explicitly passed as parameters. Static methods can contain local static variables.

Top comments (0)