A static
class is basically the same as a non-static class, but there is one
difference: a static class cannot be instantiated. In other words, you
cannot use the new
keyword to create a variable of the class type. Because there is no
instance variable, you access the members of a static class by using the
class name itself.
A static class is never instantiated. The static keyword on a
class enforces that a type not be created with a constructor. In the
static class, we access members directly on the type. This eliminates
misuse of the class.
Note:A static class cannot have non-static members. All methods, fields and properties in it must also be static.
static class StaticClass
{
//StaticClass()
{ }///Error- Static classes cannot have instance constructors
///
/// Error- 'Display': cannot declare instance members in a
static class
///
//public
void Display()
//{
// Console.WriteLine("Display");
//}
public static void StaticDisplay()
{
Console.WriteLine("Static
Display");
}
}
Call as StaticClass.StaticDisplay();
No comments:
Post a Comment