tags:

views:

1441

answers:

6

What are the benefits of having a member variable declared as read only? Is it just protecting against someone changing during the lifecycle of the class or are there any compiler speed improvements due to this keyword

+14  A: 

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.

Bill the Lizard
Also use it if you don't want to have to recompile external DLLs that reference the constant (since it gets replaced at compile time).
Ray
@Ray interesting, why not put that into an actual answer?
chakrit
Whilst I don't dispute the definition, I think from the question that ak probably understands the difference between the two. Comparison of the two would be moot - one does not have an advantage over the other - the are used in different circumstances (as I'm sure you're well aware).
Xiaofu
I don't understand why this question was marked as the answer. It doesn't answer the question. The question was not what is the dif between const and readonly - it asked why use it?
Jeff Martin
Someone else edited the question nearly two weeks after the OP accepted my answer. Apparently, the OP felt his original question was answered.
Bill the Lizard
I corrected the title
Eddie
+4  A: 

There are no apparent performance benefits to using readonly, at least none that I've ever seen mentioned anywhere. It's just for doing exactly as you suggest, for preventing modification once it has been initialised.

So it's beneficial in that it helps you write more robust, more readable code. The real benefit of things like this come when you're working in a team or for maintenance. Declaring something as readonly is akin to putting a contract for that variable's usage in the code. Think of it as adding documentation in the same way as other keywords like internal or private, you're saying "this variable should not be modified after initialisation", and moreover you're enforcing it.

So if you create a class and mark some member variables readonly by design, then you prevent yourself or another team member making a mistake later on when they're expanding upon or modifying your class. In my opinion, that's a benefit worth having (at the small expense of extra language complexity as doofledorfer mentions in the comments).

Xiaofu
And otoh desimplifies the language. Not denying your benefit statement, however.
le dorfier
I agree, but I suppose the real benefit comes when more than one person is working on the code. It's like having a small design statement within the code, a contract for its usage. I should probably put that in the answer, hehe.
Xiaofu
This answer and discussion is actually the best answer in my opinion +1
Jeff Martin
+16  A: 

I don't believe there are any performance gains from using a readonly field. It's simply a check to ensure that once the object is fully constructed, that field cannot be pointed to a new value.

However "readonly" is very different from other types of read-only semantics because it's enforced at runtime by the CLR. The readonly keyword compiles down to .initonly which is verifiable by the CLR.

The real advantage of this keyword is to generate immutable data structures. Immutable data structures by definition cannot be changed once constructed. This makes it very easy to reason about the behavior of a structure at runtime. For insntance, there is no danger of passing an immutable structure to another random portion of code. They can't changed it ever so you can program reliably against that structure.

Here is a good entry about one of the benefits of immutablity: Threading

JaredPar
+4  A: 

To put it in very practical terms:

If you use a const in dll A and dll B references that const, the value of that const will be compiled into dll B. If you redeploy dll A with a new value for that const, dll B will still be using the original value.

If you use a readonly in dll A and dll B references that readonly, that readonly will always be looked up at runtime. This means if you redeploy dll A with a new value for that readonly, dll B will use that new value.

Daniel Auger
+1  A: 

Keep in mind that readonly only applies to the value itself, so if you're using a reference type readonly only protects the reference from being change. The state of the instance is not protected by readonly.

Brian Rasmussen
+1  A: 

Be careful with private readonly arrays. If these are exposed a client as an object (you might do this for COM interop as I did) the client can manipulate array values. Use the Clone() method when returning an array as an object.

Michael