Static Classes and Static Class Members
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.
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.
Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member.
Interview Questions for Static Class
What is a static class?
A static class is a class which can not be instantiated using the “new” keyword. They also only contain static members, are sealed and have a private constructor.
What is static member?
A static member is a method, field, property or event that can be called without creating an instance of its defining class. Static members are particularly useful for representing calculations and data that are independent of object state.
3. What is static function?
A static function is another term for a static method. It allows you to execute the function without creating an instance of its defining class. They are similar to global functions. An example of a static function could be: ConvertFromFarenheitToCelsius with a signature as follows:
public static double ConvertFromFarenheitToCelsius (string valToConvert) { //add code here }
4. What is static constructor?
A static constructor has a similar function as a normal constructor i.e. it is automatically called the first time a class is loaded. The differences between a conventional constructor are that it cannot be overloaded, cannot have any parameters nor have any access modifiers and must be preceded by the
keyword static. In addition, a class with a static constructor may only have static members.
5. How can we inherit a static variable?
6. How can we inherit a static member?
When inheriting static members there is no need to instantiate the defining class using the “new”keyword.
public class MyBaseClass { MyBaseClass() { } public static void PrintName() { } } public class MyDerivedClass : MyBaseClass { MyDerivedClass () { } public void DoSomething() { MyBaseClass.GetName(); } }
7. Can we use a static function with a non-static variable?
No.
8. How can we access static variable?
By employing the use of a static member field as follows:
public class CashSales
{
//declare static member field
private static int maxUnitsAllowed = 50;
//declare method to return maximum number of units allowed
public static int GetMaxUnitsAllowed ()
{
Return maxUnitsAllowed;
}
}
The static field can now be accessed by simply doing CashSales.GetMaxUnitsAllowed(). No need to create an instance of the class.
9. Why main function is static?
Because it is automatically loaded by the CLR and initialised by the runtime when the class is first loaded. If it wasn’t static an instance of the class would first need to be created and initialised.