|
Static classes and
class members are used to create data and functions that can be accessed without
creating an instance of the class. Static class members can be used to separate
data and behavior that is independent of any object identity: the data and functions
do not change regardless of what happens to the object. Static classes can be used
when there is no data or behavior in the class that depends on object identity.
Static Classes
A class can be declared
“static”, indicating that it contains only static members. It is not possible to
create instances of a static class using the "new" keyword. Static classes are loaded
automatically by the .NET Framework common language runtime (CLR) when the program
or namespace containing the class is loaded.
Use a static class
to contain methods that are not associated with a particular object. For example,
it is a common requirement to create a set of methods that do not act on instance
data and are not associated to a specific object in your code. You could use a static
class to hold those methods.
The main features
of a static class are:
-
They only contain static members.
-
They cannot be instantiated.
-
They are sealed.
-
They cannot contain
Instance Constructors (C# Programming Guide).
Creating a static
class is therefore much the same as creating a class that contains only static members
and a private constructor. A private constructor prevents the class from being instantiated.
The advantage of
using a static class is that the compiler can check to make sure that no instance
members are accidentally added. The compiler will guarantee that instances of this
class cannot be created.
Static classes are
sealed and therefore cannot be inherited. Static classes cannot contain a constructor,
although it is still possible to declare a static constructor to assign initial
values or set up some static state.
When to Use Static Classes?
Suppose you have a class
"University" that contains
the following methods to get information about the company name and address.
class University{
public string GetUniversityName() { return @"TU Kaiserslautern"; }
public string GetUniversityAddress() { return @"Kaiserslautern
Germany
"; }
}
These methods do
not need to be attached to a specific instance of the class. Therefore, instead
of creating unnecessary instances of this class, you can declare it as a static
class, like this:
static
class
University{
public string GetUniversityName() {
return @"TU Kaiserslautern";
}
public
string GetUniversityAddress()
{ return @"Kaiserslautern
Germany"; }
}
Use a static class
as a unit of organization for methods not associated with particular objects. Also,
a static class can make your implementation simpler and faster because you do not
have to create an object in order to call its methods. It is useful to organize
the methods inside the class in a meaningful way, such as the methods of the
Math class in the
System namespace.
Class myClass{
Static VoidMain(){
Console.WriteLine(@"Unversity
Name: "+University. GetUniversityName());
Console.WriteLine(@"Unversity
Address: "+University. GetUniversityAddress());
}
}
Out Put:
University Name TU
Kaiserslautern
Kaiserslautern
Germany
|