In this article, we are going to talk about static members in C#, when and why to use them.

If you want to see complete navigation of this tutorial, you can do that here C# Intermediate Tutorial.

To download the source code, you can visit Static Members in C# Source Code. 

We are going to divide this article into the following sections:

About Static Methods

When we define a method in a class, it belongs to that class, and every instance of that class will be able to access it. One class can have many such methods. But there are some methods that are independent of the specific class instance. That kind of methods is called “static methods”. So, the static methods are the methods that don’t belong to an instance of a class, can interact only with other static elements and have the static keyword in the method description.

Let’s take the Sqrt() method for example. This method calculates the square root of a number, and we don’t have to instantiate the Math class (which the Sqrt belongs in) because this method is a static method:

So, why is the Sqrt method a static method and not a nonstatic one?

Well, the Sqrt accepts only one argument and it is enough to do its job. We provide an argument number and the method returns a square root of that number. We didn’t mention the Math class at all. That’s because we don’t have to. The Math class doesn’t provide any support to the Sqrt method to do its job. It only provides a space for the Sqrt method to reside in.

When we have a case like this one, it is usually a good solution to create a method as a static one.

Working with a Static Method

To call a static method, as we said, we don’t need an instance of a class. We can call it with the following syntax: ClassName.MethodName(arguments…);

So, when we want to use the Sqrt method or any other method from the Math class, we can call it like this:  Math.Sqrt(16);

Creating a Field by Using the Const Keyword

If we prefix our field with the const keyword, we can declare such a field where its value can never change. The keyword const is short for constant. A constant member is defined at compile time and it can’t be modified at runtime.

We can create a const variable in the following way: AccessModifier const Type Name = Value;

Const member - Static Members in C#

Static Class

In C#, next to static methods we can declare static classes as well. The static class can contain only the static members. Its purpose is to act as a holder for the utility methods and fields. There is no point in instantiating this type of classes by using the new keyword. Furthermore, we can’t do that at all. But we can create a default constructor as long as it is a static one. Any other type of constructor is illegal:

About Extension Methods and How to Use Them

Let’s suppose that we want to add a new feature to the string type, for example, the FirstLetterUpperCase functionality that always makes the first letter of a string with upper case. We can write a normal method for that purpose:

But, as we can see, we need to send a word as a parameter every time and to accept returned value every time as well. This is not the wrong approach but we can do it even better. There’s where the extension methods come in.

An extension method enables us to extend an existing type with additional static methods. We must create that kind of methods inside a static class and they have the first parameter prefixed with the this keyword.

But why do we have to place a prefix in front of the first parameter?

Because that parameter is an indicator that tells to the compiler which type we extend.

So here is the previous example but with the extension method:

Extension methods result - Static Members in C#



We are done with the static members and now we have a great tool in our toolbox that we can use while developing our C# applications.

In this article, we have learned:

  • How to use static classes
  • The way to use static methods
  • How to create extension methods
  • About const keywords and creating constants

In the next article, we are going to talk about Anonymous Types and Nullable Types in C#.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.