New Features of C#2.0 - Static Classes


Static classes are used when a class provides functionality that is not specific to any unique instance. Here are the features of static classes in C# 2.0.

  • Static classes can not be instantiated.
  • Static classes are sealed so they can not be inherited.
  • Only static members are allowed.
  • Static classes can only have static constructor to initialize static members.

Advantages

Compiler makes sure that no instance of static class is created. In previous version of C#, the constructor has to be marked private to avoid this from happening.

Also compiler makes sure that no instance members are declared within a static class.

Sample:

Public
static class MyStaticClass
{
Private
static int _staticVariable;
Public
static int staticVariable;
{
Get
{
Return _staticVariable;
}
Set
{
_staticVariable =
value;
}
}
Public
static void Function()
{
}
}


Recommended Free Ebook
Similar Articles