static is a most confusing when its asked in exams and interviews and they might be the deciding factor sometimes for some companies.
Think this question
OK! I have a static class that implements an interface… and do you think its possible?
The actual answer is “NOPE” and here I am trying to describe the functionality of
static in C#.
static class is real
Still some don’t know we have static classes in C# those who come to C# from Java, they hate Java and they hate Java because they have to define an outer class and inside it they can have a nested static class, simply like
but this is useless because this
static doesn’t really mean what the
static is. This is just like you don’t need to create an object for A to instantiate B. So with this
B b = new B();
is a valid statement and you don’t really need to do
A a= new A();
B b = a.new B();
And again they hate Java because to achieve the real
static class in Java, they have to make a
final class or make a
private constructor for the class or make all class members and functions
static. This is a huge pain, so they hate Java. But in C# we just directly have
Eeay piece right.. C# is sweet.
But don’t forget you can’t have instance members inside an
static class all members should be
See the above example. As most people say
DateTime is a best example for static classes, they are created when we need static object for utility classes.
To summerize this I can say, static class are
- Only contains static member.
- Can’t be inherited.
- To be sealed.
- Can’t contains instance constructor, it can be contains only static member & Private constructor.
override, virtual, abstract in
static member can not be marked as
override, virtual, abstract this prevents the
static member polymorphism and ensures the static functionality. Since static members are defined on the type, not the instance, of a class, they must be called explicitly on that type. So when you want to call a method on a child class, you need to use its name to call it. This makes inheritance irrelevant.
3. Some interesting method calls, static base class methods
Look at the below code
And do you think this is possible?
I bat, is possible. As you can see, the call is made to A.Test, because it was the A class that defined it, and not to B.Test, even though you can write the code that way.
4. It really does’t make sense for a static abstract classes and interfaces
Yes it really doesn’t make sense because Inheritance in .NET works only on instance base. Static methods are defined on the type level not on the instance level and this is why overriding really doesn’t work as I said in (2).
Static classes can not have instance constructors, but and instance class may have an static constructor. still static classes can have static constructor. Why have static constructor is to hold any static data. There is no other use actually. But denote the point that any static constructor in invoked before the default constructor. You can look at the below example
The output of this will be
- These are some interesting points to remember when you are considering static constructors
- A static constructor does not take access modifiers or have parameters.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
6. Private static methods
Private static methods are useful sometimes, most of the time in helper / utility classes. They can be used methods that are exposed to outer world but yet not exposed to the outer world by them selves.
We can also have static properties as well, its with the same concept of properties that have static functionality. You can get a good picture on it with the below code
I hope I cover the most, If I am missing anything just comment below. Happy coding 🙂