We are going to discover what happens when we mark something as static. In C#, the static modifier can be marked against classes, functions, variables and properties amongst other things. But what is this static modifier? And how does something mark static behave? That's discover.

What is Static and Non-Static?

When you mark something as static, it means it's specific to that type and not to a particular instance of that type. That means you don't have to create an instance of that type to use it if it's marked as static.

When the static modifier is absent, it means an instance of that type has to be created using the new keyword in C#. Failure to do so will result in a NullReferenceException.

Constructors in Static and Non-Static Classes

With a static class, you can have one parameterless constructor. That's it! This gets called when your application references the class in some way. Perhaps you are setting a variable with a type of a static class. The first bit of code that will run is the static constructor.

With a non-static class, you can have multiple constructors with different parameters. These must be called using the new keyword in C#.

Can I have a Mixture of Static and Non-Static Methods in a Class?

If you mark a class as static, you cannot create a separate instance of it. So with that in mind, all methods will need to be marked as static inside a static class.

But, if you create a class without the static modifier, you can use static methods alongside it. When you create an instance of a class, you can read and write to any static method within that class, assuming the keywords to that method allow you to do this. So you can have a mixture of a static and non-static methods in a non-static class.

Bear in mind that if you include a static constructor in a non-static class, that static constructor will be called before any instance is created. But the static constructor will only be called once, whereas, a non-static constructor will be called every time an instance of that type is created.

What Happens to the Value in a Static Property?

Assuming you are running a web application. A web app runs a separate thread every time to get a resource such as a web page or an image. If you set a value against a static field or property, that value will be shared across all threads for the lifetime of the application.

If you wish to have separate values for each thread, you can use the [ThreadStatic] attribute against a static field. This will set a different value per thread.

That would not be same with a non-static field or property. A non-static field or property would have to belong to a non-static class, and an instance of that class would have to be created per thread.

Bringing the Differences to Life

It's quite difficult to explain the differences between Static and Non-Static classes and methods in word format. So I have created a YouTube video with a working example for you to get a better understanding on how they both behave. Enjoy!

About Dave

I am a .NET developer, building web applications in .NET Framework and .NET Core with a SQL Server database.

Some of the .NET packages I have used include Entity Framework and MVC.

I've also used many JavaScript frameworks such as React and jQuery, and have experience building CSS in SASS.

Related Tags

C#
Class
Method

Twitter Feed