Static Keyword: Instantiation, Usage, and Best Practices

Understanding Static Keyword

Static is a keyword that makes it instantiate only once internal when the static class is consumed first.

Instantiating Classes

In the class without static, we have to instantiate to use, but we can instantiate many times and store different data on each instance.

namespace BasicCsharp
{
    public class Customer
    {
        public string? name { get; set; }
        public string? email { get; set; }
        public string? phone { get; set; }
        public string computer = "";
        public Customer()
        {
            computer = System.Environment.MachineName;
        }
    }   
}

This is the customer class that gets the customer's information.

Using Static and Non-Static methods

We will move the System.Environment.MachineName; to a different class and use with static and without static to see the difference.

namespace BasicCsharp
{
    public class CompName
    {        
        public string getName()
        {
            return System.Environment.MachineName;
        }
    }
}

Making Classes Static

After making this public class, we can now instantiate this class in the Customer class and use the getName method. 

namespace BasicCsharp
{
    public class Customer
    {
        public string? name { get; set; }
        public string? email { get; set; }
        public string? phone { get; set; }
        public string computer = "";
        public Customer()
        {
            CompName computerName = new CompName(); 
            computer = computerName.getName();
        }
    }
}

We can use the getName() method in CompName after instantiate. and with this method, we should follow OOP principles and also can store different data if they instantiate more CompName class.

But we can make the CompName class static and the getName method static then we will be able to use them without instantiate manually.

namespace BasicCsharp
{
    public static class CompName
    {
        public static string getName()
        {
            return System.Environment.MachineName;
        }
    }
}

Making CompName class static and getName method static

namespace BasicCsharp
{
    public class Customer
    {
        public string? name { get; set; }
        public string? email { get; set; }
        public string? phone { get; set; }
        public string computer = "";
        public Customer()
        {
            computer = CompName.getName();
        }
    }   
}

We can simply call the getName method without instantiating the CompName class. static class will be instantiated internally since it only instantiates once. they all share data and methods. 
and when you make a class static, new is prohibited. 

Static classes are fixed value and fixed methods that are going to be used in the application. so if it is not a fixed method or value it should not be a static class.

This static class is cannot be affected by inheritance or polymorphism. 

Next Recommended Reading How to use this keyword in static method