C#: Read-only fields

The C# keyword readonly is used when you don’t know a value at compile time, but you want that value to never change once it’s been assigned. Read-only fields aren’t static like constants are; if you’d like to make them static you must include the static keyword.

An interesting fact about readonly fields is that their value can be assigned from within a constructor. An example of this is when you need to read a value from the database.

Leave a Reply

Your email address will not be published. Required fields are marked *