In C# what is the difference between:
public static class ClassName {}
And:
public class ClassName {}
Static class can contain static members only.
Static member can be used without instantiating a class first.
All methods/properties in a static class must be static, whereas a 'normal' class can contain a mix of instance and static methods.
You can't instantiate (create objects of) a static class. And it can only contain static members.
Example: System.Math
A static class cannot be instantiated, and can contain only static members. Hence, the calls for a static class are as: MyStaticClass.MyMethod(...)
or MyStaticClass.MyConstant
.
A non static class can be instantiated and may contain non-static members (instance constructors, destructor, indexers). A non-static member of a non-static class is callable only through an object:
MyNonStaticClass x = new MyNonStaticClass(...);
x.MyNonStaticMethod(...);
A static class also can not be inherited from, whereas a non-static class with static members can be inherited from.
public static class ClassName {}
A static class is just like a global variable: you can use it anywhere in your code without instantiating them. For example: ClassName. After the dot operator, you can use any property or function of it.
public class ClassName {}
But if you have non-static class then you need to create an instance of this class. For example:
ClassName classNameObject = new ClassName();