Static class, static method and static constructor are some common questions which are asked in most interviews. A static class is basically the same as a non-static class, but there in one difference, a static class cannot be instantiated, means we cannot use new keyword to create an object of the class. Even we cannot create any parameterized constructor because to pass the constructor value we need to create object, we will check all these points in more detail in this article.
When we can use static class?
We have a class Math in C#, let’s check it’s methods and operation. Say Math.Min, it takes two values of different data types and return minimum one, similarly Max, Round, Floor, Ceiling. If you notice all these methods take some values and operate only those provided values. So a static class should be container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields.
What is a static class?
A static class is similar to normal class with these differences
- Contains only static members means all the methods and members must be static
- Cannot be instantiated by using new keyword
- By default it is sealed class and therefore cannot be inherited.
- It can have default constructor or
- Can have only one constructor without any parameter
- Access modifiers are not allowed on static constructors
- Cannot have instantiate constructors
- Methods can be called by using class name dot (.) method name.
What is a static Method?
Static method are similar to regular (instantiate) method, but they can be accessed directly by using the class name rather than the instance of the class, it will give error if we will try to access them with class instance, see it with this example:
public class Sample
{
public void DisplayInst()
{
Console.WriteLine("I am a instance method");
}
public void DisplayStatic()
{
Console.WriteLine("I am a static method");
}
}
Call these two methods with instance and directly, see this
var obj = new Sample();
obj.DisplayInst(); // I am a instance method
obj.DisplayStatic(); // Wrong, it cannot be called
// Let’s try to call them directly
Sample.DisplayInst(); // Wrong, it cannot be called
Sample.DisplayStatic(); // I am a static method
Can we have static as well as instantiate constructor in a class?
- Yes, it can be in a normal instantiate class
- Any number of instantiate constructors
- With zero to any number of parameters
- Can have only one static constructors
- Static constructor cannot have any parameter
- Static constructor are mostly used to initialize some values
We will create a simple Car class with different constructors and a static constructor to understand the feature, let’s create a class with two instantiate constructor, one static constructor and two Detail methods
public class Car
{
String _color = "White";
static decimal _price = 25000;
static Decimal _discount = 100;
public Car()
{
}
public Car(String color)
{
this._color = color;
_discount = 500;
}
static Car()
{
_discount = 1000;
}
public String Detail()
{
return String.Format("Price: {0:C} and Color: {1} "
, _price - _discount, _color);
}
public static String Detail(String color)
{
return String.Format("Price: {0:C} and Color: {1} "
, _price - _discount, color);
}
}
It is valid and working class which has both types of constructors and methods, if you can see there is a normal constructor without any parameter and one static constructor without any parameter, because static constructor cannot have any parameter. Let's try to execute them one by one
- var car = new Car(); car.Detail();
- Output: Price: $25,000 and Color: White
- var car = new Car(“Black”); car.Detail();
- Output: Price: $24,500 and Color: Black
- One parameter constructor is called and assign the discount $500
- Car.Detail(“Red”);
- Output: Price: $24,000 and Color: Red
- When we will call Detail method, constructor will be called before executing the method.
Try to add a new constructor like this and you will get following error
public static Car(Int32 color) { }
- a static constructor must be parameterless
- access modifiers are not allowed on static constructors
Conclusion:
- Easy to use
- No need to create instance before calling the methods
- Cannot have more than one constructor
- Constructor cannot have any parameter
- Access modifier cannot be use with constructor
- Constructor is called before calling any method internally
- Cannot be instantiated
- Cannot be inherited because by default it is sealed
No comments:
Post a Comment