C# Statics and How They Work

In this post, I discuss statics in C#, how they work, and some interesting uses for them.

Advertisements

Disclaimer: (I know the picture for this post isn’t C#, I just found it on some free image library site and thought it looked trendy)
In this post I want to discuss a topic that most are probably familiar with, and some may not know about, statics.

What is a static?

A static in C# has a few different meanings depending on the context that the static keyword is used in. In general, statics apply to a class or a class level variable. What does this actually translate to though? Well, when you declare something as being static, it means that you can use said thing without declaring an instance of your class…at least you don’t explicitly have to declare an instance of your class. Let me explain this really quick. Whenever you write a normal class, it gets complied into a dll and loaded at runtime, and nothing is really done with it until you declare an instance of the class, at which point the class, and all of it’s methods and members are instantiated. This isn’t really how a static works.

So how do they work?

We often think of statics as having one shared instance, and that’s basically correct. An object in C# is just a pointer to a grouped section of memory, whenever you reference a method or member, you’re just offsetting the pointer to the object to get to the portion of memory you care about. Since a static just has one shared reference, what this translates to in a program is essentially a global pointer, whereby you access the global pointer by calling the class name and then the static field. If you apply static to a class, it means that only one instance of the class may exist at any one time, if you’ve ever used C++, you’ll note this is basically just a singleton behind the scenes. This is different from normal static fields, because a normal static field does not require any instance of the class to exist at all, the static field is a stand alone thing.

Side effects of statics

Statics are garbage collector roots, plain and simple. What that means for those that don’t know, is that a static basically will never be garbage collected. This matters, because let’s say we declare a bitmap as an array and for whatever reason we load a 50mb image, once we load that bitmap, it’s not able to be freed; we can unload the pixel data, but we have to do it explicitly, we can’t just rely on the garbage collector to do it for us. Statics also get messy when they are being used in multi-threaded scenarios, but frankly I don’t know enough to speak to this topic, so I advise you do some independent research on this topic (as will I now that I think about it, and I’m actually going to add this to the list of things to blog about) as it does affect your software.

Other uses for statics

Statics can also be used in a using statement. For example, I can say something like:

using static Android.Hardware.Camera;

By doing this, we don’t have to reference the class name to access static methods or members of the class, we can simply use them as if they were locally declared variables or methods.

Interestingly enough, constants are implicitly static, which is why you can’t declare a constant as being static.

You can also declare a classes constructor as static, this is a special constructor that runs before any instances of the class are instantiated, basically it allows you to initialize static fields, and other things before the static fields ever get used.

Conclusion

Well, there’s not a lot more to discuss, I mean there’s certainly more to statics, but anything more than what I’ve posted about is probably going to be more niche, as I can’t think of anything else I use in my day to day work. If you have anything you feel should be added, or if I explained something incorrectly, please feel free to bash me for it. Thanks for reading, until next time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Advertisements
Advertisements
%d bloggers like this: