© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
9 replies
mellowzippy

✅ 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();
    }
}
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();
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

readonly struct and readonly struct instance members (optimization?)
C#CC# / help
4y ago
✅ Return readonly property
C#CC# / help
2y ago
✅ methods
C#CC# / help
8mo ago
✅ Methods
C#CC# / help
16mo ago