Sunday, May 17, 2009

Static Class

We can declare a static class. We use static class when there is no data or behavior in the class that depends on object identity. A static class can have only static members. We can not create instances of a static class using the new keyword. .NET Framework common language runtime (CLR) loads Static classes automatically when the program or namespace containing the class is loaded.

Here are some more features of static class
• Static classes only contain static members.
• Static classes can not be instantiated. They cannot contain Instance Constructors
• Static classes are sealed.

What is static member of class?
A static member belongs to the class rather than to the instances of the class. In C# data fields, member functions, properties and events can be declared static. When any instances of the class are created, they cannot be used to access the static member.
To access a static class member, use the name of the class instead of an instance variable
Static methods and Static properties can only access static fields and static events.

Like: int i = Car.GetWheels;
Here Car is class name and GetWheels is static property.

Static members are often used to represent data or calculations that do not change in response to object state.

No comments:

Post a Comment