C# interview questions - Static class in c# : Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends today's question for you tell me something about a static class or we can say what is the static of us well static class can have only static member it cannot be inherited and instantiated why it cannot be inherited instantiate it because it is internally sealed and abstract let's check it out I'm going to check intermediate language code using il DSM tool I already am tool go to the exe and see seed and abstract so static class is decorated internally with see an abstract keyboard what is the use of seed and absurd cute seal when we didn't create any class with the seal keyword it stopped that class to be inherited that class cannot be inherited anyway an abstract class abstract class store that has to be instantiated so the static class neither can be inherited nor instantiated that feature it gets from the sealed and abstract so we know why the static class cannot be instantiated cannot be inherited the answer is yes it is sealed and abstract and it can have only static member only static members they're known as static member is not a lot in static class see there are some points that we need to remember while creating static class when you get aesthetically there are a few things that we should remember always we need to explicitly declare static every member if it is not decorated with a static keyword it will be considered instance member say with example static class is already created I am creating a member and EMP ID employee ID this is the member of static class C let's build it cannot declare instance member in a static class if we do not decorate this with static keyword it is considered instance member so decorate with a static class sorry a static keyword build Wilda started succeeded there is no will succeed so every member even in static class we declared that must be decorated with the static keyword we cannot declare any local field static explicitly because it is implicitly a static what is what you what is the meaning of local field a local type [Music] school name is equal to local field meaning the scope whenever you declare any variable the scope of the variable in local meaning that is accessible within the method is considered local variable so this is the local variable declared within the class and I haven't decorated with the static keyword because any local variable declare within a static class is implicitly static so if I declare a static keyword here say I'm not getting here intelligence to declare static even if I declare it I will get build the static is not valid for this item and here I will not declare the static so it is getting so static is not valid for this item right the static is not validated with this item so meaning of second paragraph is we cannot declare any local field static explicitly because it is implicitly a study so this is clear let's move to the next question can we have constructor in static class yes we can have a static constructor static class and that should be parameter less that should not be parameter all easy to see let's first declare a parameterised what is it mean by parameterize string name this is the parametric instructor see tell me if we compile it now will it give it me error yes because I haven't decorated with the static keyword now it will again give me there because the static constructor cannot have parametrized constructor see a static constructor must be a parameter list so remove this parameter we'll succeed so yes the static class can have only parameterless constructor why it's I should have parameter less constructors because we cannot instantiate any static class so the whatever value we pass into the argument suppose string name the construct when we come when we instantiate and then we pass our value to the argument then it executes like this say employee well this is a static instructor must be parameterless this is the parameter list cause it should be parameterless constructor because we cannot instantiate and we cannot call this constructor with by using a new keyword so this is the reason it is parameter less always and you know why we cannot instantiate because it is a decorated with the abstract keyword static internally abstract cannot have any modifier any and parameter it is always parameter leska so if we know it is parameter less constructor what does it mean by modifier see modifier mean public suppose declared public access modifier not a lot of statical start out in the static constructor a static constructor must be parameterless constructor these two both paragraph is covered now let's move in our next video we will discuss scenario based question like if the static class can have a static constructor right we know static class can have a static method if you call method only will they say to construct execute you will see in this in our next video thanks like and subscribe for more videos
Info
Channel: Simple Coding
Views: 8,026
Rating: undefined out of 5
Keywords: static class in c#, C# interview questions, can we create object of static class, constructor in static class
Id: 3RrRxYimjkA
Channel Id: undefined
Length: 7min 50sec (470 seconds)
Published: Sun May 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.