DEV Community

Cover image for ๐Ÿค” CONST vs READONLY in C#? (Explanation)
ByteHide
ByteHide

Posted on • Updated on • Originally published at bytehide.com

๐Ÿค” CONST vs READONLY in C#? (Explanation)

Who does not have doubts when starting to learn something new?๐Ÿค”

All the world

In this case, when someone starts programming in .NET they usually get a questions that even many Senior Developers donโ€™t know how to answer correctly:

What are the differences between READONLY and CONST?

They are the same?

If a variable that can only be read and not changed is assumedโ€ฆ Could the definition of constant be applied to it?

I answer you:

NO โŒ

Okay butโ€ฆ If they are not the sameโ€ฆ What is their difference?

Well, letโ€™s start with the basics:

At first, they may seem the same if we take into account that the outputs are similar, but, we must understand the real differences between them since if we do not use them well we could have many problems in our projects.


1. Read only

To define a constant in C# we will use this ๐Ÿ‘‡

public const double PI = 3.14;
Enter fullscreen mode Exit fullscreen mode

With the const modifier we tell the compiler that the variable we are assigning will be a constant. In this way, it can only be initialized when it is declared.

If we want to modify it, the compiler will tell us no and show us this error:

exit code

In the case of defining a read-only variable, it is similar ๐Ÿ‘‡

public readonly double PI = 3.14;
Enter fullscreen mode Exit fullscreen mode

And again, if we try to modify it, it will give us an error again, but not the same one:

dev

โœ… Conclution:

Although both of us have given us errors, it has not been the same. In this case the difference is that the readonly classes CAN be modified, only as long as the constructor of that class is not executed.

What did I mean by that?

Simply being inside the constructor we can modify them if we want, for example, obtaining their value from parameters that we pass to the constructor. And no, that is not possible in the case of the constant.


2. Static members

The second most notable difference is that a constant is ALWAYS a static member of the class to which it belongs.

Letโ€™s go back to practice, if we write:

public class Class01{ public const double PI = 3.14;}
Enter fullscreen mode Exit fullscreen mode

And then we try to access the value of PI, it would not be necessary to create a new object of the class, that is ๐Ÿ‘‡

Console.Write("The Pi value is " + Class01.PI);
Enter fullscreen mode Exit fullscreen mode

We would not need to instantiate anything at any time, since being a static member, common with the other objects of that class and clearly existing in its definition.

If we want to do the same with a read-only variable, we would have to add the static modifier. It would look like this ๐Ÿ‘‡

public class Class01{ public static readonly double PI = 3.14;}
Enter fullscreen mode Exit fullscreen mode

So we could use it in the same way as the constant.


3. Compilation

Is everything clear at the moment? ๐Ÿค—

Now we are going to โ€œdig inโ€ a little more.

Suppose we have a basic class defined like this:

using System;
namespace ConstantTestLIB
{
     Public class Class01
     {
          Public static readonly double readonlyPI = 3.14;
          Public const double constantPI = 3.14;
     }
}
Enter fullscreen mode Exit fullscreen mode

Basically, as in the first example, we define a single class with the two static members ๐Ÿ‘‡

  • Readonly Pi
  • Const Pi

We generate a .dll so that we can use it in other applications.

Now, we create a new project and add a reference to the DLL created earlier. And we just show an output on screen like here:

using System;
using ConstantTestLIB;
namespace ConstantTestCMD
{
     class Program
     {
          Static void Main (string[] args)
          {
          Console.WriteLine(โ€œThe constant is: โ€ + Class01.ConstantPI 
          + โ€œand the readonly is: โ€ + Class01.ReadOnlyPI);
          }
     }
}
Enter fullscreen mode Exit fullscreen mode

If we run this, the result will be ๐Ÿ‘‡

Image description

Okay, all right, right?

Now letโ€™s imagine that we compile and ship our new console application to our clients.

Ohh wait, the PI value is longerโ€ฆ๐Ÿ˜ฐ

Okay, we just change the DLL code, compile it and send it back to it ๐Ÿ‘

It would simply be necessary to overwrite the new DLL with the old one and everything would work without any problem. But โ€ฆ Letโ€™s run the program again with the new DLL. You gonna crash like Windows 10 blue screen๐Ÿ‘‡

Image description

Only the value of the read-only variable has been changed and not the constant.

If we examine the generated code with a decompiler, we will find this ๐Ÿ‘‡

Image description

With this we can realize that, even though they are a member in a class that belongs to external DLL, the constants are compiled literally.

โœ… This is VERY simple to explain:

When the compiler parses the code, seeing that we are using a constant and it cannot be changed, it simply changes it to the real value on all sides, even if we compile it with an external DLL.

In the case of read-only variables, we can see that it is still used from the DLL.

At this point we come to the main problem that you can find. If you declare a read-only variable and you want to change its value, overwriting the DLL does not work, you will have to compile all the applications that use it again.


Finally, I hope that with this article I have clarified the most important differences between read-only variables and constants.

Thus being able to avoid possible problems and a few more compilations, which I think we already waste a lot of time on it. ๐Ÿค—

Top comments (2)

Collapse
 
oliverqueen006 profile image
Oliver Queen

Thanks buddy for this. It clears some dust from that topic. Thanks again.

Collapse
 
yeah69 profile image
Dima

Nice article.

Just for completion. In C# there is even a third way to declare an immutable element: get-only property.