Here are some of the basic and important points that need to be kept in mind while working with Static Class.
- We can create a static class or member by using Static keyword.
- Static class or member can be used when the object of the class or member does not change every time.
- Static class or member are loaded at the time of namespace are loaded by CLR.
- We cannot create an instance of the static class because the static class member is called by class Name.
- Static class only contains static members.
- Static member can’t access non static members.
- Static class cannot be inherited because it is by default Sealed Class.
- Static class cannot be marked as private.
- Static class cannot inherit or implement the class or interface. Because static class only inherits system. Object class by default.
- Static constructors are used to initialize static member.
- Static constructors have no access modifier.
- Static constructors have no parameters (parameterless).
- A non-static class can have static as well as a non-static constructor.
- Static constructor is called before the non-static constructor.
- Static members cannot be marked as override, virtual and abstract.
Here are some of the questions with their answer about static class and static members.
1) How can we create a Static Class or Member?
Ans: By using static keyword.
2) What is the real use of Static Class or Member?
Ans: We can create Static Classes in our project for a common routine purpose that object is not changed i.e. a new object not created. And its members are easily accessible by class name. In a real project, we generally create a utility project that contains the static classes for example logging and tracing, send mail, common type conversion and so on.
We can also see a built-in static class like Math that has static member like sqrt(), abs() etc.
3) Can we create an object of the static class?
Ans: No, because generally we create an object of the non-static class for accessing the member of that class but in the static class, we can access or call a static class member by class Name.
4) Can we declare non-static member inside a static class?
Ans: No, because the non-static member can be modified at runtime and it is required to create an instance if public.
5) Can Static member access non-static members?
Ans: No, But if we create an instance of a non-static class then we can access those members.
6) Can we mark a Static class as private?
Ans: No, because if we marked a Static class as private then, what is the use of that class? Means how can we access its members.
7) Can a class inherit a Static class?
Ans: No, because by default static class is sealed.
8) Can we declare sealed to a Static class explicitly?
Ans: No, because by default static class is sealed, so we can use only one from sealed or static at a time.
9) Can a Static class inherits or implement to other class or static class?
Ans: No, because by default static class must be inherited from System.Object class means we can only inherit System.Object class.
10) What is the use of Static class constructor?
Ans: To initialized static member.
11) Different between static and non-static constructor signature?
Ans: Static constructor does not have access modifiers and has no parameters on the other hand non-Static constructor can have access modifiers and can have parameters.
12) Can we create a non-static constructor in a static class?
Ans: No.
13) Which constructor called first non-static constructor or static constructor?
Ans: The Static constructor is called first and then non-static constructor.
14) How many time static constructor execute?
Ans: Only one time.
15) What is the difference between the static class of a singleton or private constructor?
Ans: Static class by default is sealed class and also static class can not inherit other types like class or interface.
whereas singleton class we make sealed but singleton class can inherit other types like class or interface.
Summary:
So, Guys this is all about Static Class.
I Hope in this post covered all the points about Static Class .
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.