✅ Readonly methods

My VSCode is telling me to change my methods to readonly. I can't find a good explanation anywhere as to what this does and why it's recommended. So I'd really like an explanation from someone who knows.

public struct Note(Book book, List<Entry> entries) : IEnumerable<Entry>
{
    public Book book = book;
    public List<Entry> entries = entries;

    public Note(Book book) : this(book, []) { }

    public Note(Book book, Entry entry) : this(book, [entry]) { }

    public readonly void Add(Entry entry)
    {
        entries.Add(entry);
    }

    public readonly void Remove(string title)
    {
        foreach (Entry entry in entries)
        {
            if (entry.Title == title)
            {
                entries.Remove(entry);
            }
        }
    }
    
    public IEnumerator<Entry> GetEnumerator()
    {
        return entries.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
Was this page helpful?