Static Classes
Up to this point, classes have been considered non-static classes. These classes were used to create instances of the type.
Static classes are like non-static classes, except you cannot create instances of the type and the class cannot contain any instance members. A static class can only contain static class members.
Declaring a Static Class
To declare a static class, you will use the static
keyword.
Declaring Static Class Members
Static classes cannot contain instance members, only static members or constants.
C#
public static class Math
{
public const double PI = 3.1415;
public static int Minimum(int a, int b)
{
}
}
Using Static Classes
To reference members of a static class, you must use the class identifier with dot notation.
C#
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Math.PI);
Console.WriteLine(Math.Minimum(3, 5));
}
}