Sreedhar
Sreedhar

Reputation: 30045

private const vs public readonly

I got three class files where in constant is declared as

private const string PAGE_SIZE = "PageSize";

Is it good to move this to new file to hold all common constants declared as

public readonly string PageSize = "PageSize";

What are the pros and cons for this?

Upvotes: 5

Views: 23991

Answers (4)

Martin Liversage
Martin Liversage

Reputation: 106826

There are some important differences between a const and a readonly field:

  1. The const is evaluated at compile time. If you declare the const in a separate assembly that you reference from you application a change to the const will only affect the application if it is recompiled using the updated assembly. To quote from the .NET Design Guidelines for Developing Class Libraries:

    Do use constant fields for constants that will never change.

    For example, the Math class defines E and PI as static constants.

    The compiler inserts the values of const fields directly into the calling code, which means that const values can never be changed without the risk of introducing a compatibility issue.

  2. A readonly field can be initialized at run-time enabling you to perform run-time calculations to compute the value and use. A const can only be declared by a constant expression that can be fully evaluated at compile time. The only reference type that can be const is String.

About your specific question it really depends on how these constants are used. Obviously you shouldn't have multiple definitions of the same constant. Otherwise it is probably easier to understand if the constant is declared "near" where it is used, e.g. in the class or even the method where it is used.

Upvotes: 10

Tim Barrass
Tim Barrass

Reputation: 4939

It might be that you're optimizing too early.

This might mean losing flexibility by making the constants shared and public -- hard to say without knowing what the existing classes are.

What if PageSize actually needs to vary between them in the future? Does it actually need to be synchronised across all three classes, or is this just a small tweak that seems like a good idea at the moment?

Upvotes: 0

Aidan
Aidan

Reputation: 4891

The two may have similar practical effects, but they are useful for signalling your intentions.

So, a const value is something that is available to all instances of your class and will never change. A readonly signals that you have a data value that could be different for each instance of you class, but will be immutable once the class is created. Immutability can be a really useful guarantee when you are sharing the instance of the class between different consumers. In passing, in CLR Via C#, Richter prefers readonly public members to properties with only public setters, I'll have to dig it out and remind myself why.

Upvotes: 1

Jeremy McGee
Jeremy McGee

Reputation: 25200

Performance considerations aside -

  • In favour: your constants are all centralised in one place.

  • Against: your constants are no longer close to the point at which they're used.

For constants that are shared between classes it makes sense to break them out to a common single class so they're only specified once. However this implies "inappropriate coupling", so it might be that all the logic that uses this constant needs to be in the same class.

Upvotes: 1

Related Questions