A Static class is a class that cannot be instantiated i.e. its object cannot be created.
Static class
• Has the keyword static.
• Cannot be instantiated and no object can be created.
• Keyword new is not supported.
• Objects and Functions can be accessed by class name.
• Mandatory for functions and methods to be marked static i.e. can contain only Static members.
• Members cannot be accessed using this keyword.
• Only one instance can be created.
Example:
public static class B
{
public static object E;
public static void F()
{
}
public static void G()
{
//Valid
var q = E;
//Valid
F();
}
}
Non-Static class
• Does not have the keyword static.
• Can be instantiated and object can be created.
• Keyword new is supported.
• Objects and Functions cannot be accessed by class name unless marked static.
• Not mandatory for functions and methods to be marked static i.e. can contain Static as well as Non-Static members.
• Non-Static members can be accessed using this keyword. But Static members cannot be accessed using this keyword.
• Multiple instances can be created.
Example:
public class A
{
public object C;
public static object G;
public static void D()
{
//Invalid
var p = C;
//Valid
var q = G;
//Invalid
E();
}
public void E()
{
//Valid.
var p = this.C;
//Valid
var q = G;
//Valid
D();
}
}