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
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?
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();
var car = new Car(“Black”); car.Detail();
Car.Detail(“Red”);
Try to add a new constructor like this and you will get following error
public static Car(Int32 color)
{
}
Conclusion:
![]() |
Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
|
By Ali Adravi On 01 Jan, 15 Viewed: 23,726 |
We will discuss here what is IEnumerable, IEnumerator, what are differences between them, where should we use IEnumerable and where to IEnumerator, once we will finish our discussion it will be clear, which one would be best for which situation and why, so let’s see it with example To better... By Ali Adravi On 29 Mar 2013 Viewed: 44,580
In this article we will discuss about interface in C# with real time example, it properties, implementations and pros and cons as well. 1. An interface can never be instantiated e.g. ICar car = new ICar(); It’s wrong. 2. It only contains signature of its methods. 3. An interface has neither... By Ali Adravi On 18 Aug 2013 Viewed: 47,535
Method overloading means having different methods with same name but with different types of parameters or number of parameters also known as static polymorphism. In this article we will try to understand what is method overloading and how we can overload a method with example. Suppose we have a... By Hamden On 19 Jul 2013 Viewed: 3,124
Polymorphism is derived from two Latin words, 1. Poly means many 2. Marphose means forms. By using inheritance, a class can be used as many types, say we have a Person which may be a Professor, may be a Student, may be Clerk. So in this article we will see some example of Polymorphism. When we... By Hamden On 01 Jun 2013 Viewed: 4,548
Sometime we need to convert string to an enum type, say I need to store my enum value into viewstate then we cannot store enum or any object to viewstate so converted that to string and store it, and on postback need to read the viewstate and convert to enum, so how to convert a string into enum?... By Hamden On 21 May 2013 Viewed: 1,542