c# interview questions:-Prove that only 1 instance of the object is created for static classes?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the most discussed topic in cheese shop is static classes and the most discussed point about static classes are how many instances of static classes created you know when static classes are invoked the answer is but obvious it's only one so irrespective how many clients are calling the static classes right static class you know the you know only one instance of the object is created so in this video what we will do is we'll try to solidify this principle and let's practically see how many instances of static classes actually exist in memory so what we're going to do as you can see on your video in a via we have created actually a very simple console application and I have named this console application as I will prove static classes have one instance so let me go and just press ENTER over here and let's actually see practically that only one instance of static class actually exists in memory so there's our console application so let's go ahead and create a simple static class in this console application here so let's type public static so static is a keyword by which we can create the static class and I'm going to name this class as my class here okay so there's my class created we need to actually give the class keyword there so public static class my class right now what we'll do in the static class is we go ahead and create a simple constructor here okay so let me go ahead and create a constructor here called as public my class and let me compile this now this will show up by error why because you know everything inside a static class should be static so it's either constructor it's either methods it's either variables or anything so you can see that it's saying that the constructor should be static okay so let me go and correct my defect or correct my errors over here so I'm going to just say okay public static right now again it sure it should show me errors why because by default you know the static static classes constructors are always public so you don't need to specify modifiers are saying private public or something so we need to go and delete that public keyword which is assigned to the constructor now let's go and build everything should be fine right now what we'll do is let's go and call this static class you know inside the static void mean now in order to in order to invoke this class right we need some functions and some methods here so let's go ahead and create a method in the in the my class let us call this method as some method so again you know because either methods either variables inside the static class should be static so we are defining the some method as static now let's go in static void main and invoke the some method which you have created let me make it as public so that it's you know we are able to invoke it in thread static void main so I'm going to invoke it twice right now what should happen is if only one instance of the object is created right the constructor the static my class constructor should fire only one one time okay so in other words even though I am calling this some method twice here the constructor of my class should be executed only once so if the static my class constructor is executed only once what it signifies is that we know only one instance of the object is created right so so if only one instance of the object is created then this static my class constructor will run only once right right so constructor should run once now let us put the necessary diva points here so let me put a debug point here let me put the debug point on the constructor to see that how many times it runs let me build a solution everything fine let's go and run this Fi alright now the first time any method is called on my class you can see that the my class constructor is executed and which signifies that you know one instance you know our instance of the object is created right good now let me just move this breakpoint ahead now let's you know now the second instruction in which again the Ransom method you can see that at this moment you know again the constructor is not running in other words it's using the same instance of myclass right so this proves the point that you know basically static classes have only one instance you know in memory in order to you know in order to further solidify this point what we'll do is let's go ahead and create a variable called encounter here in my class again you know the variable has to be static here so we need to actually define the I count variable as static right so that ya now now what what we'll do is we'll go and increment this I croc count variable twice so you can see that I have written two commands they're saying okay increment + 1 and again increment + 1 okay and let's display the value of I count now because only one instance of the object is there right what will happen is this I count variable red will become a global variable in other words it should show me the value as 2 okay in case it was an instance variable or attached to a particular object or in case in other my class is having new instance of the object then I will see the value s1 but you can clearly see that you know the eye count variable is now a global variable why because only one instance of myclass is running right so you know so this demonstration you know you can see that it proves that basically only one instance of the object exists you know for static classes and that was clearly proven you know when we saw that the constructor only fired once II respect you you know how many calls were made to the methods of the static class so I hope this enjoyed this you know I hope you enjoyed this video so in this video the whole point was to prove that static class has only one instance created and that instance of the object actually serves all the clients thank you very much now whatever video you have seen right is just a glimpse of what we have done so in case you are interested in our video package you can go to our site that is W correspond comm you can call on this number and you can ask the complete DVD package what we have so in this DVD package what we are done is basically we have covered almost everything what a.net developer wants so right from basics of asp.net object of programming SQL server to new technologies like WCF silverlight linq azure entity framework we also have uml architecture estimation project management there is a complete invoicing project end to end which is covered so that you can get a better feel of how to actually create projects in a systematic manner we have covered server products you know both for sharepoint 2007 as well as for 2010 we have lot of desperate this video on SQL server etc so this complete package you know you can get from w luke respond calm if you're interested and you can call on this number and you can ask for the rates it's it's a very decent rate what we have on the same in the same way you know as compared to the videos we also have one more product with us that is our interview question books so we have different kinds of interview question books you know right from from dotnet interview questions to SQL server interview questions sharepoint interview questions biztalk interview questions etc so in case you are interested in the books part you can you can call on these numbers as per your location so you can see these numbers on the board at this moment so I hope that you keep enjoying the videos you keep seeing our site and I hope that you gain more knowledge thank you very much
Info
Channel: .NET Interview Preparation videos
Views: 183,479
Rating: undefined out of 5
Keywords: c# interview, static classes, csharp, csharp interview questions, step by step c# video, interview questions for csharp, static class csharp, static class in csharp, static classes c#, Why do we need static class in C#?, static classes interview questions, Static classes questions, What is the advantage of static class in C#, c# instances, static classes in c#, use static classes, csharp programming, what is static classes, using static classes in c#, .NET and c# tutorial
Id: N9xwjZ4mMvw
Channel Id: undefined
Length: 7min 29sec (449 seconds)
Published: Thu Sep 08 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.