Thoughts on using readonly for any / all members of an instance of a class?

Introduction

For some time now I have been using readonly modifier for almost all fields of the class. I use it for List <T> members, IDisposeable members, ints, string, etc ... all but the types of values ​​that I intend to change. I usually do this even when I would usually like to exclude a member in Dispose (). IMHO. The benefits of not needing if statements to check for null or remote conditions far outweigh the "potential" for problems in objects that can be "deleted" several times.

Question

When do you use readonly or use?

Do you or your company have any recommendations and / or coding standards regarding the use of readonly?

I am curious to hear your thoughts on the next class of designs, is the general concept a good practice or not?

class FileReaderWriter : IFileReaderWriter, IDisposable { private readonly string _file; private readonly Stream _io; public FileReaderWriter(string path) { _io = File.Open(_file = Check.NotEmpty(path), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); } public void Dispose() { _io.Dispose(); } ... } 
+7
c # coding-style
source share
6 answers

I use readonly on fields in any situation where the code compiles successfully.

Why? Simple, if the value / value of a field suddenly shifts from never changing to changing, it can violate subtle assumptions in class and consumers. Therefore, I want to be warned about this change in order to evaluate the consequences of this new behavior.

+8
source share

Like you, I use readonly wherever I can. If possible, I also use immutable collections.

Dispose is interesting because it effectively mutates the type from “usable” to “unusable” - I will be tempted to have unreadable bool text to indicate this. Again, I rarely have to perform IDisposable . Usually, when I use resources, this is only for the course of one method, so I make the resource local to this method.

+3
source share

The readonly keyword is another tool in your .NET tool. Therefore, use it where it is used! In general, you want to achieve the least amount of access to properties,

The reason is because you want to protect your code from calling / access from places where you did not expect it to be available. Therefore, returning to your readonly question, if you have a member of a class that only needs to be changed (set) once from the constructor, then readonly is the way to do this.

+1
source share

Our company does not have official readonly coding standards, but it has universally agreed with our team to use it whenever possible, and where it makes sense (which is often). It clearly indicates the intention of the programmer or specification that it was not intended to change after it was installed.

Some of us also use Resharper, which usually reminds you to use it quite a bit. So your sample class would be what would be considered “good practice” in our team.

0
source share

For shorter code, I do not put it, but only if I have a static analysis tool that will protect against misuse.

For example, when writing in Java, I rely on PMD to make sure that I don't shoot in the leg without declaring all my parameters final.

You could correctly say that using readonly / final would rather tell you about your mistake, but I do it long enough for my common sense to go away while writing, and if I miss something, the static analysis tool reminds me of this .

0
source share

Like several other replicas, I often use readonly . Fortunately, Resharper is very interested in determining when members can be readonly . It shows the code reader that this value is set after the instance is created, which is a valuable detail of imo.

0
source share

All Articles