views:

220

answers:

5

I am working on a project using ReSharper. On occasion it prompts me that a field can be made readonly. Is there any performance or other benefit to this? I am presuming the benefits would be quite low-level, or would any benefits be purely semantic?

Thanks

With example below the field was initially just private, but resharper prompted to set it as readonly. I understand the reason why it can be set as readonly, ie. its being set in the constructor and not changed again, but just wondering if there are any benefits to this...

public class MarketsController : Controller
{
    private readonly IMarketsRepository marketsRepository;

    public AnalysisController(IMarketsRepository marketsRepository)
    {                
        this.marketsRepository = marketsRepository;
    }
}

Edit What is the easiest way to look at the MSIL?

+9  A: 

The benefit is purely semantic. It will help users of your code explicitly understand that this field can't be changed after object is created. Compiler will prevent unwanted changes of this field. I totally agree with following quote from Python Zen:

Explicit is better than implicit.

Some details:

The only difference between normal field and read-only field is flag initonly in IL. There is no optimization about it (as with constants) because actually it allows all operations (get and set, but only in ctor). It is just hint to compiler: don't let it be changed after construction.

.field public initonly int32 R
Andrey
+1: Being explicit with regards to readonly fields helps massively when reasoning about multithreaded systems. When you add an instance field to any class, you should think about whether it should change or not. If it doesn't, mark it as readonly - simple as that. In fact, I sometimes think there should be a 'mutable' keyword rather than 'readonly' (nod to F# and its parent languages).
Alex Humphrey
+1  A: 

You might be interested in this answer.

The readonly keyword is used to declare a member variable a constant, but allows the value to be calculated at runtime. This differs from a constant declared with the const modifier, which must have its value set at compile time. Using readonly you can set the value of the field either in the declaration, or in the constructor of the object that the field is a member of.

rmx
+6  A: 

It's not so much low-level performance, but more high-level maintainability. Making things readonly is one of the possibilities you have to limit and control the number of places a certain value can be changed. This in turn means that you reduce interdependency between classes (a.k.a. "loose coupling"); the result is an application that has fewer internal dependencies and thus a lower complexity. In other words, readonly fields and properties make your application more maintainable.

tdammers
+1  A: 

It also might help spotting some bugs as well. The value is assigned in a construcotor only and this could be a problem if you forgot to change elsewhere or not. And if it is not supposed to be changed then you mark it as a read only.

Al Bundy
+1  A: 

My professor taught me back in the day that declaring something readonly is a way of admitting to your computer that you make mistakes.

Rice Flour Cookies
Debugging is admitting you made a mistake.
qstarin