C# Topics: Static Classes
Like a variable or a method, a class can be made static. A static class:
Introduction#
Like a variable or a method, a class can be made static. A static class:
- Is a class whose members must be accessed without an instance of the class. In other words, the members of a static class must be accessed directly from (using the name of) the class, using the period operator
- Is a class whose members must be created as static. In other words, you cannot add a non-static member to a static class: all members, except for constants, must be static
Creating a Static Class#
To create a static class, precede the class keyword with the static keyword. Based on the above two rules, here is an example:
using System;
public static class Square
{
public static double side;
public static double Perimeter()
{
return side * 4;
}
public static double Area()
{
return side * side;
}
}
public class Exercise
{
public static int Main()
{
Square.Side = 36.84;
Console.WriteLine("Square Characteristics");
Console.Write("Side: ");
Console.WriteLine(Square.side);
Console.Write("Perimeter: ");
Console.WriteLine(Square.Perimeter());
Console.Write("Area: ");
Console.WriteLine(Square.Area());
return 0;
}
}This would produce:
Square Characteristics
Side: 36.84
Perimeter: 147.36
Area: 1357.1856
Press any key to continue . . .If you create a class marked as static, you cannot derive a class from it.
Static Constructors#
Like a normal method, a constructor can be made static. There are rules you must follow. If you want to use a static constructor, you must explicitly create it (the compiler doesn't create a static constructor for you). The static constructor must become the default constructor. That is, you must create a constructor that doesn't take any argument. Here is an example:
public class Person
{
public string firstName;
static Person()
{
}
}If you create a static constructor, you cannot directly access the non-static fields of the class: You can still access any field of the class as you see fit. Here are examples:
using System;
public class Person
{
public string firstName;
static Person()
{
}
}
public class Exercise
{
public static int Main()
{
Person pers = new Person();
pers.firstName = "Gertrude";
Console.WriteLine("Personal Identification");
Console.Write("Name: ");
Console.WriteLine(pers.firstName);
return 0;
}
}To use a static constructor, you have various options. To initialize a member variable in the static constructor, you can declare a variable for the class and access the member variable. Here is an example:
public class Person
{
public string firstName;
static Person()
{
Person pers = new Person();
pers.firstName = "Gertrude";
}
}In reality, one of the reasons for using a static constructor is to initialize the static fields of the class or take any action that would be shared by all instances of the class. Therefore, another option to use a static constructor is to initialize the static member variables. After doing this, when accessing the initialized static field(s), it(they) would hold the value(s) you gave it(them). Here is an example:
using System;
public class Person
{
public static string firstName;
static Person()
{
firstName = "Gertrude";
}
}
public class Exercise
{
public static int Main()
{
Console.WriteLine("Personal Identification");
Console.Write("Name: ");
Console.WriteLine(Person.firstName);
return 0;
}
}This would produce:
Personal Identification
Name: Gertrude
Press any key to continue . . .Because the constructor is static, you cannot access it by declaring a variable for the class. Another rule to observe with a static constructor is that you must not add an access modifier to it. The following will result in an error:
public class Person
{
public static Person()
{
}
}You can create a class that uses a combination of a static constructor and one or more other (non-static) constructors. Here is an example:
public class Person
{
public string firstName;
public string lastName;
static Person()
{
}
public Person(string first, string last)
{
firstName = first;
lastName = last;
}
}If you create such a class and if you want to declare a variable for it, the default constructor doesn't exist or is not available. If you want to declare a variable, you must use a constructor that takes an argument. Here is an example:
using System;
public class Person
{
public string firstName;
public string lastName;
static Person()
{
}
public Person(string first, string last)
{
firstName = first;
lastName = last;
}
}
public class Exercise
{
public static int Main()
{
Person pers = new Person("Gertrude", "Monay");
Console.WriteLine("Personal Identification");
Console.Write("First Name: ");
Console.WriteLine(pers.firstName);
Console.Write("Last Name: ");
Console.WriteLine(pers.lastName);
return 0;
}
}This would produce:
Personal Identification
First Name: Gertrude
Last Name: Monay
Press any key to continue . . .Based on this, you should create a static constructor only if you have a good reason. Home Copyright © 2010-2011 C# Key

