46. Read-only properties#
In the previous chapter, we introduced read-only fields and discussed how they provide a level of flexibility that constants cannot offer. In this chapter, we’ll learn that we can achieve the same effect by using a read-only property, meaning an auto-implemented property that only have a ‘get’ accessor. It is common practice in C# to use read-only properties instead of read-only fields.
In C#, read-only properties are properties that can only be set at the time of object creation. Once the object is constructed, the value of a read-only property can’t be changed. Practically, this means that the read-only property must be assigned a value at the time of declaration or in the constructor of the class, and after this, it cannot be changed.
A read-only property in C# is defined like any other property, but with only a get accessor. Without a set accessor, the property becomes read-only, because there’s no way to change the value of the property.
Consider the Book
class, from the chapter on read-only fields, where each book has a unique and constant ISBN
. Here’s how you might use a read-only property to represent this:
public class Book
{
public string ISBN { get; }
// Initial value can be set upon instantiation or initialization only.
public Book(string isbn)
=> ISBN = isbn;
}
In this example, ISBN
is a read-only property. The ISBN is passed into the Book constructor when a new Book
object is created, and the ISBN
property is set. After that point, the ISBN
property can’t be changed.
public class Book
{
public string ISBN { get; }
public Book(string isbn)
=> ISBN = isbn;
// This method does not compile.
public void ChangeISBN (string ISBN)
=> this.ISBN = ISBN;
}
(10,12): error CS0200: Property or indexer 'Book.ISBN' cannot be assigned to -- it is read only
Remember that the intention behind a read-only property is to guarantee that the value remains constant after the object’s construction.
Warning
The same dangers that we talked about in the chapter on read-only fields also apply to read-only properties. While read-only does mean that the property cannot be reassigned a new value, it does not mean that the value it has been assigned itself cannot change.
Remember the analogy of a jar with a bag of marbles from the chapter on read-only fields? Even though we cannot replace the bag of marbles with another bag we can change the number of marbles in the bag, without changing the bag.