what is the use of static |static class in c#|static class with example
If class having with static keyword it becomes static class. Static class can have static function static variable and static properties. We can't create an object of static class and also we can't have sub-classes of static class.(no inheritance)
Static class contains only static member and static function if they are not static we will not able to call any thing of static class.
Static classes with static data members are nothing more than global variables. All static classes are sealed (you cannot derive them from a static class).
So, the question comes in mind, if we can't create object and can't inherit then what is the use of static class.(See Below)
public static class Employee // Static class
public static string name;
public static int age;
public static string work()
static void Main(string args)
Employee.work(); //Able to call by the class name
Employee.age = 21;
Employee.name = "SOMEONE NAME";
Why we use static class:
When we have requirement to achive something like global in our application and we don't want to create an object of class again and again (because object creating is consumption of memory) then we have to go for static class. Simply we write class name and use (.) operator, we can call the members and functions available in static class.
Note: Static class is control by CLR and CLR provides the memory to the static class it means that use have no control over the static class.
Static class is behaves like a service or helper classes.