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ó

Returning Read-only References from Functions in C#

Dániel Szabó

  • Mar 27, 2020
  • 7 Min read
  • Mar 27, 2020
  • 7 Min read
and Tools
Programming Languages


In this guide you will learn a concept for writing type and memory safe code. With C# version 1.0 developers could pass arguments to methods by reference. As time passed and programming changed the world, new challenges rose that forced new features to be added to C#. In version 7.0 of C#, two very important features were added: the possibility to declare references to local variables the ability to return by reference from methods.

This guide will build a foundation for you to understand the two types of argument passing, namely by value and by reference, and clarify what structs are and how to return them from methods or functions.

Value Versus Reference

There are two main types of argument passing in C#: reference and Value. C# also has the ability to define custom types, but that is not in the scope of the guide. Value and reference types show different performance characteristics because of the nature of their implementation. Value types do not have any overhead in terms of memory because the data that you can store in a specific type like int, string, or double has a specific size. Reference types have two extra fields called ObjectHearder and MethodTable.

ObjectHeader is used by the CLR to store additional information, and it's basically a bitmask. MethodTable is a pointer to the Method Table, which is a set of metadata about a specific type. Calling a method from an object makes the CLR jump to the Method Table and get the address of the method's implementation to perform the call.


You can find a more detailed guide on structs here. For now, all you need to be aware of is that structs are very similar to classes but they are intended for storing data. Read-only structs are a bit different because their fields can only be modified once, when the constructor of the struct is called. After that a read-only struct stays intact. This guide is about read-only references, so read-only structs fall out of the picture.

This is an example of a read-only struct:

1public readonly struct Pandemic
2    {
3        public string virusName { get; }
4        public double infectRatio { get; }
5        public int initialCases { get; }
6        public double deathRate { get; }
7        public Pandemic(string name, double ratio, int cases, double rate)
8        {
9            virusName = name;
10            infectRatio = ratio;
11            initialCases = cases;
12            deathRate = rate;
13        }
14    }

We see that the accessors are only defined with get, set is missing, and the readonly keyword is there to tell the CLR that the struct is going to be immutable. This means that over time the fields cannot be changed.

Now, you will need to combine the struct and passing values by reference to create a demonstration for the original topic of the guide.

Ref Read-only

The ref keyword indicates to a function that the value is passed by reference. It can be used in multiple contexts. For example, it can be used in a method signature and method call to pass an argument to a method. Or it could be used in a method signature to return a value to the caller by reference, which is what is needed at this point. This keyword allows you to notify the caller that the returned object is immutable, and modification to the value is not possible.

Take a look at this simple example before diving into read-only structs:

1using System;
3namespace Pluralsight
5      public class SimpleRefReadonly 
6    {
7        static int[] a = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
9        static ref int GetIndex(int index) {
10            return ref a[index];
11        }
13        public static void Main()
14        {
15            Console.WriteLine(GetIndex(0));            
16            Console.ReadKey();            
17        }
18    }

This code has a static array of integers with a size of 10. GetIndex, the method used here, is taking an index as an argument and returns an item from the array, which is read-only. If you pass an index bigger than the length of the array or smaller than zero you'll get an exception stating that a System.IndexOutOfRangeException exception occurred during runtime.

For a more complex example, you can build a struct that represents a school with rooms that have names and sizes. The struct must not allow modification to the size of the rooms once they are initialized.

1using System;
3namespace Pluralsight
5    public struct ClassRooms {
6        public ClassRooms(string name, int size) {
7            Name = name;
8            Size = size;
9            _currentUtilization = null;
10        }
11        private ClassRooms[] _currentUtilization;
12        public string Name { get; set; }
13        public int Size { get; set; }
14        public override string ToString()
15        {
16            return $"{this.Name} :: {this.Size}";
17        }
18        public void SetRoom(ClassRooms[] classrooms) => _currentUtilization = classrooms;
19        public ref readonly ClassRooms Getutilization(int x)
20        {
21            return ref _currentUtilization[x];
22        }
23    }
24      public class ComplexRefReadonly 
25    {                
26        public static void Main()
27        {
28            var Rooms = new ClassRooms[] { new ClassRooms("Mathematics", 20), new ClassRooms("Biologs", 15) };
29            var School = new ClassRooms();
30            School.SetRoom(Rooms);
31            Console.WriteLine(School.Getutilization(1));
32            Console.WriteLine(School.Getutilization(0));
33            Console.ReadKey();            
34        }
35    }

Upon executing the example, the following should be on the console:

1Biologs :: 15
2Mathematics :: 20

Here you have a struct called ClassRooms that represents the school. This struct is not read-only! Each classroom has a name and a size that need to be passed to the constructor. The ToString() function has been overridden to allow each classroom to be printed prettier. The key here is the following function:

1public ref readonly ClassRooms Getutilization(int x)
2    {
3        return ref _currentUtilization[x];
4    }

This function returns a read-only copy of a specific classroom without modification, and protects the original values assigned during initialization. The Main() function is responsible for building up the school; first the array of Rooms variables is initialized, then the School, which is made up of rooms. Finally the Getutilization(<index>) function allows retrieval of a read-only copy of the given classroom.


In this guide we went over the difference between value and reference types. We clarified what ref readonly means in concept, and throughout the code demonstration we showed how to combine them in order to create type and memory safe application. I hope this guide has been informative to you and I would like to thank you for reading it!