Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Dániel Szabó

Benefits of Read-Only Coding in C#

Dániel Szabó

  • Feb 24, 2020
  • 6 Min read
  • 6,420 Views
  • Feb 24, 2020
  • 6 Min read
  • 6,420 Views
Languages Frameworks and Tools
C#

Introduction

Most languages support read-only programming or coding with keywords, literals, operators, etc. It is, however, a very divisive subject. Some people start developing in C# as their first language, while others jump to this language with development experience in other languages. The result is differing presumptions about this coding style.

In this guide, we will see what read-only coding boils down to in C# and how you can safely utilize it to your advantage. There are two keywords that support read-only programming, const and readonly, and we will break down each of them, then assemble a small app for demonstration.

const

With the help of the const keyword, we are able to declare a constant field or constant local. These are not treated as variables and they cannot be modified; their value stays intact during the whole execution cycle. We should avoid creating constants for storing information we expect to change over time, because when the change happens, an exception will stop us.

There are four types of constants:

  1. Number
  2. String
  3. Bool
  4. Null reference

You may declare your constants in C# the following way.

1const int a = 10;
2const string b = "My Constant!";
3private const string Name = "Daniel";
4private const int Salary = 100000;
csharp

You also have the option to declare multiple constants on the same line.

1const double a = 1.0, b = 2.0, c = 3.0;
csharp

Due to the restrictions the const implies and also forces upon the developer, a new keyword was introduced.

readonly

The 'readonly' modifier can be used in a total of four contexts:

  1. Field declaration
  2. Readonly struct declaration
  3. Readonly member definition
  4. Ref read only method return

When we use the field declaration context, we need to know that the only time the assignment can occur is when it is declared or when the constructor of the same class is invoked.

1using System;
2
3namespace Pluralsight
4{
5    public class ReadOnlyProgramming
6    {
7        readonly string Name;
8        readonly int Age;
9        readonly int Salary;
10        public ReadOnlyProgramming()
11        {
12            Name = "Default Name";
13            Age = 99;
14            Salary = 10000;
15        }
16        public ReadOnlyProgramming(string name, int age, int salary)
17        {
18            Name = name;
19            Age = age;
20            Salary = salary;
21        }
22        public static void Main()
23        {
24            ReadOnlyProgramming a = new ReadOnlyProgramming();
25            ReadOnlyProgramming b = new ReadOnlyProgramming("Florian", 28, 200000);
26
27            Console.WriteLine($"The variable: {nameof(a)} holds: {a.Name} with age: {a.Age} and salary: {a.Salary} USD/yr !");
28            Console.WriteLine($"The variable: {nameof(b)} holds: {b.Name} with age: {b.Age} and salary: {b.Salary} USD/yr !");
29            //a.Name = "Naomi"; // uncommenting this results in an error
30            Console.ReadKey();
31        }
32    }
33}
csharp

The following output is produced when the code executes.

1The variable: a holds: Default Name with age: 99 and salary: 10000 USD/yr !
2The variable: b holds: Florian with age: 28 and salary: 200000 USD/yr !
bash

We have two constructors in our class. One without argument sets a default value for the readonly properties after it is called, and the second constructor takes three arguments and initializes the properties with them. This is allowed, but if we were to modify any of the properties marked with readonly after the instance is created, the following error would welcome us.

1A readonly field cannot be assigned to (except in a constructor or a variable initializer)
bash

When we use the readonly struct context, the rule is that we will have an immutable struct where all the instance fields need to be marked as readonly, otherwise the following error welcomes us.

1Instance fields of readonly structs must be readonly.
bash

Let's create a struct to store static server information about our infrastructure.

1using System;
2
3namespace Pluralsight
4{
5    public readonly struct Server
6    {
7        public readonly string Name;
8        public readonly string Location;
9        public readonly string OS;
10        public readonly int vCPU;
11        public readonly int RAM;
12
13        public Server(string name, string location, string os, int vcpu, int ram)
14        {
15            Name = name;
16            Location = location;
17            OS = os;
18            vCPU = vcpu;
19            RAM = ram;
20        }
21
22    public override string ToString() => $"(Name: {Name}, Location: {Location}, Operating System: {OS}, vCPU: {vCPU}, RAM: {RAM} GB)";
23    }
24
25    public class ReadOnlyProgramming
26    {
27        public static void Main()
28        {
29            Server a = new Server("Domain Controller","Amsterdam","Centos 7.6",8,16);
30            Console.WriteLine(a.ToString());
31            Console.ReadKey();
32        }
33    }
34}
csharp

The output produced is as follows.

1(Name: Domain Controller, Location: Amsterdam, Operating System: Centos 7.6, vCPU: 8, RAM: 16 GB)
bash

There are two important concepts here. We cannot add new property without making it read-only. This prevents the data from being modified, and we cannot alter the existing properties, either.

When we use the readonly member context, we have the option to apply the keyword on members. We cannot apply it on class or interface member declarations, however. There are two rules you need to remember:

  1. Static methods or properties cannot be read-only.
  2. Constructors cannot be read-only.

A small demonstration of a read-only member for our above example would be.

1public readonly DateTime InstallDate { get; }
csharp

The ref readonly context is a bit more complicated, and it involves pointers and unsafe code. The modifier applied on ref means that the returned reference cannot be modified. Any type that can be returned by ref can have the read-only modifier applied to it.

Conclusion

Read-only coding is all about securing information in either class fields or structs. Making sure we do not allow tampering with data is an ever-growing concern in our digital world. C# has the necessary features to help you write secure applications. I hope this guide has been informative to you and I would like to thank you for reading it!