Hello readers, In this tutorial, I am going to cover a very important topic “static class” in C#. A static class is a class that is declared using the static keyword and cannot be instantiated. Let’s discuss static class in detail.

What is a static class in C#?

Like any other programming languages C, C++, and Java, C# also introduces the concept of static class. A static class is a class that is declared using the static keyword.
A static class can not be instantiated that means we cannot use the new keyword to create the object of the static class. A static class must contain only static member. The main advantage of using a static class that it guarantees that the instance of the class cannot be created. Since we cannot create an instance of a static class and a static class can only have a static member so to access the member of static class we use class name itself. For example, if I have a class called MyStaticClass that has a public method called MyStaticMethod then to access the member of a static class you need to write the following line of code.

MyStaticClass.MyStaticMethod();

Why we need a static class:

A static class does not allow a user to create an instance of the class as well as it restricts the user to inherit the class. So static class make your implementation simpler, safe and faster because you do need to have to create an object in order to access the member of the class.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
namespace StaticClassDemo
{
 class Program
 {
 public static class MyStaticClass
 {
 public static int myStaticVariable = 0;

public static void MyStaticMethod()
 {
 Console.WriteLine("This is a static method.");
 }

public static int MyStaticProperty { get; set; }
 }


 static void Main(string[] args)
 {

Console.WriteLine(MyStaticClass.myStaticVariable);
 MyStaticClass.MyStaticMethod();
 MyStaticClass.MyStaticProperty = 100;
 Console.WriteLine(MyStaticClass.MyStaticProperty);
 Console.ReadLine();
 
 }
 
 }
}

Output:

0
This is a static method.
100

 

Point of interest for static class:

  • you cannot create an instance of a static class.
  • A static class must contain only static members.
  • A static class is implicitly sealed class and therefore a static class cannot be inherited.
  • A static class cannot have instance constructor because we cannot instantiate a static class.
  • Access modifiers are not allowed for a static class.
  • To access the member of static class we use class name {.} method name.

View More:

Conclusion:

I hope you would love this post. Please don’t hesitate to comment for any technical help. Your feedback and suggestions are always appreciated.

Thank You.

Explore post tags

50 Comments

  • “Hi! I just want to give you a huge thumbs up for your excellent info you’ve got right here on this post. I will be returning to your site for more soon.”

  • Woah! I’m really loving the template/theme of this blog. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between superb usability and appearance. I must say you have done a amazing job with this. Also, the blog loads very quick for me on Opera. Excellent Blog!

  • Pretty section of content. I just stumbled upon your weblog and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Anyway I will be subscribing to your augment and even I achievement you access consistently quickly.

  • An outstanding share! I’ve just forwarded this onto a colleague who was doing a little research on this. And he actually ordered me lunch due to the fact that I stumbled upon it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanx for spending some time to discuss this matter here on your internet site.

  • I absolutely love your blog and find almost all of your post’s to be just what I’m looking for. Do you offer guest writers to write content for yourself? I wouldn’t mind composing a post or elaborating on a few of the subjects you write with regards to here. Again, awesome web log!

  • I’m really impressed together with your writing skills as smartly as with the format in your blog. Is that this a paid topic or did you customize it your self? Either way keep up the excellent quality writing, it’s uncommon to peer a nice blog like this one today..

  • I don’t even understand how I finished up here, but I believed this put up was good. I do not understand who you are however certainly you are going to a famous blogger when you are not already 😉 Cheers!

  • The next time I read a blog, Hopefully it does not fail me just as much as this one. I mean, I know it was my choice to read through, however I genuinely thought you would probably have something interesting to say. All I hear is a bunch of crying about something you could fix if you weren’t too busy looking for attention.
    http://www.trade-dress.com/board_ZXVh85/1942240

  • Thank you for every other informative site. Where else may just I get that type of information written in such a perfect means? I have a project that I am just now operating on, and I’ve been on the glance out for such info.

  • You’ve made some really good points there. I looked on the web for additional information about the issue and found most individuals will go along with your views on this site.|

  • Hello there, just changed into alert to your blog via Google, and located that it is really informative. I’m going to watch out for brussels. I’ll be grateful if you happen to continue this in future. A lot of people will be benefited from your writing. Cheers!

  • Fantastic items from you, man. I’ve take into accout your stuff previous to and you’re simply extremely magnificent. I actually like what you have obtained here, certainly like what you’re stating and the way in which wherein you assert it. You’re making it entertaining and you still care for to keep it smart. I cant wait to learn far more from you. That is actually a terrific site.

  • The next time I read a blog, I hope that it doesn’t fail me just as much as this one. I mean, I know it was my choice to read through, but I actually thought you would have something helpful to say. All I hear is a bunch of moaning about something that you could possibly fix if you were not too busy searching for attention.
    http://studyforum.com.au/forums/users/terencee74/

  • After going over a number of the blog articles on your web site, I really like your way of blogging. I added it to my bookmark site list and will be checking back soon. Please check out my web site as well and tell me what you think.|

  • Thanks , I’ve recently been looking for info approximately this subject for ages and yours is the greatest I have came upon so far. But, what in regards to the conclusion? Are you certain in regards to the source?

  • Next time I read a blog, Hopefully it won’t disappoint me just as much as this one. After all, I know it was my choice to read, but I genuinely thought you would probably have something interesting to talk about. All I hear is a bunch of whining about something that you could possibly fix if you were not too busy searching for attention.
    https://i-f.club/forums/users/marcelampn/

  • It’s really a nice and useful piece of information. I am happy that you just shared this helpful information with us. Please stay us informed like this. Thank you for sharing.

  • I am really impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, it’s rare to see a nice blog like this one nowadays..

  • Hello there! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading through your blog posts. Can you suggest any other blogs/websites/forums that deal with the same subjects? Thanks!

  • Asking questions are actually pleasant thing if you are not understanding something entirely,
    however this paragraph provides fastidious understanding yet.

  • I’m extremely inspired along with your writing talents as smartly as with the structure in your weblog. Is that this a paid topic or did you modify it yourself? Anyway stay up the nice high quality writing, it’s rare to see a great blog like this one nowadays..|

  • I appreciate, cause I found exactly what I was looking for. You have ended my four day long hunt! God Bless you man. Have a nice day. Bye

  • I do believe all the concepts you’ve presented on your post. They are really convincing and can definitely work. Still, the posts are too short for newbies. Could you please prolong them a bit from next time? Thanks for the post.

  • It is perfect time to make a few plans for the long run and it is time to be happy. I’ve read this submit and if I may I want to recommend you few interesting issues or advice. Maybe you can write subsequent articles referring to this article. I want to read even more things about it!

  • You made some really good points there. I looked on the internet to learn more about the issue and found most individuals will go along with your views on this website.

  • We absolutely love your blog and find nearly all of your post’s to be precisely what I’m looking for. can you offer guest writers to write content available for you? I wouldn’t mind writing a post or elaborating on some of the subjects you write with regards to here. Again, awesome weblog!|

  • Can I simply just say what a relief to discover someone who truly understands what they’re talking about on the net. You certainly understand how to bring a problem to light and make it important. A lot more people really need to look at this and understand this side of your story. I was surprised you aren’t more popular given that you most certainly have the gift.
    https://johns382bwr1.losblogos.com/profile

  • An outstanding share! I’ve just forwarded this onto a coworker who had been conducting a little research on this. And he in fact ordered me breakfast because I found it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanks for spending some time to talk about this matter here on your blog.

  • Can I simply say what a comfort to find somebody who truly understands what they’re discussing over the internet. You actually understand how to bring an issue to light and make it important. A lot more people have to read this and understand this side of your story. It’s surprising you are not more popular because you definitely possess the gift.
    https://elderp036esf5.p2blogs.com/profile

  • Hello there, You’ve done a fantastic job. I will certainly
    digg it and personally suggest to my friends. I’m sure
    they’ll be benefited from this site.

  • May I simply just say what a relief to discover someone who actually understands what they’re talking about over the internet. You actually realize how to bring an issue to light and make it important. A lot more people should read this and understand this side of your story. I can’t believe you’re not more popular since you most certainly possess the gift.
    https://dalton602zn.blogdomago.com/1632415/facts-about-?????-????????-revealed