What is a static class

C# - Static

C# includes static keyword just like other programming languages such as C++, Java, etc. Thestatic static keyword can be applied on classes, variables, methods, properties, operators, events and constructors. However, it cannot be used with indexers, destructors or types other than classes.
The static modifier makes an item non-instantiable, it means the static item cannot be instantiated. If the static modifier is applied to a class then that class cannot be instantiated using the new keyword. If the static modifier is applied to a variable, method or property of class then they can be accessed without creating an object of the class, just use className.propertyName, className.methodName.
Example: C# Static class
  1. public static class MyStaticClass
    {
        public static int myStaticVariable = 0;
    
        public static void MyStaticMethod()
        {
            Console.WriteLine("This is a static method.");
        }
    
        public static int MyStaticProperty { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.WriteLine(MyStaticClass.myStaticVariable);
    
            MyStaticClass.MyStaticMethod();
    
            MyStaticClass.MyStaticProperty = 100;
    
            Console.WriteLine(MyStaticClass.MyStaticProperty);
        }
    }

Output:

  1. This is a static method100


In the above example, MyStaticClass is a static class with static variable, method and property. All the static members can be access using className without creating an object of a class e.g. MyStaticClass.MyStaticMethod().

For example, consider the following myNonStaticClass with mix of static and non-static methods:

Example: Static members in non-static class

  1. public class MyNonStaticClass
    {
        private static int myStaticVariable = 0;
    
        public static void MyStaticMethod()
        {
            Console.WriteLine("This is static method.");
        }
    
        public void myNonStaticMethod()
        {
            Console.WriteLine("Non-static method");
        }
    }

C# Static Constructor

A static or non-static class can have a static constructor without any access modifiers like public, private, protected, etc.
A static constructor in a non-static class runs only once when the class is instantiated for the first time.
A static constructor in a static class runs only once when any of its static members accessed for the first time.
Example: Static Constructor in Static Class

  1. public static class MyStaticClass
    {
        static MyStaticClass()
        {
            Console.WriteLine("Inside static constructor.");
        }
        public static int myStaticVariable = 0;
    
        public static void myStaticMethod()
        {
            Console.WriteLine("This is static method.");
        }
    
        public static int MyStaticProperty { get; set; }
    
    }
    
    class Program
    {
    
        static void Main(string[] args)
        {
            
            MyStaticClass.myStaticVariable = 100;
    
            MyStaticClass.MyStaticProperty = 200;
    
            MyStaticClass.myStaticVariable = 300;
    
            MyStaticClass.MyStaticProperty = 400;
    
        }
    }

OutPut:-

  1. Inside static constructor



Post a Comment

0 Comments