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(); } ... }
csharptest.net
source share