Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 811925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:07:18+00:00 2026-05-15T01:07:18+00:00

There seem to be three choices for implementing publicly accessible constants in C#. I’m

  • 0

There seem to be three choices for implementing publicly accessible constants in C#. I’m curious if there are any good reasons to choose one over the other or if it’s just a matter of personal preference.

Choice 1 – private field plus property getter

private const string _someConstant = "string that will never change";

public string SomeConstant
{
    get { return _someConstant; }
}

Choice 2 – property getter only

public string SomeConstant
{
    get { return "string that will never change"; }
}

Choice 3 – public field only

public const string SomeConstant = "string that will never change";

Which do you recommend and why?


Update

Apparently, this has turned into a discussion of whether to use const or static readonly. Not exactly what I intended, but it did teach me that Choice 3 is definitely a bad idea because, if the value of the const changes in a future version, it requires all referencing assemblies to be recompiled.

However, I don’t think anyone has really discussed Choice 2 yet. I’m still curious if there’s any disadvantage with having just a getter that returns a value and nothing else.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:07:19+00:00Added an answer on May 15, 2026 at 1:07 am

    Choices 1 and 2 are equivalent, really.

    It seems to me there are really three different situations:

    • You know for certain that the string will never, ever change. In this case it’s reasonable to make it const. (For example, Math.PI is const. That’s not going to change any time soon.) There are some subtle memory implications in doing this over using static readonly, but they’re very unlikely to affect you. You shouldn’t do this if the value may change and you don’t want to recompile all callers in that situation, for the reasons given elsewhere. Note that for many projects (particularly internal corporate ones) it’s really not a problem to recompile all callers.

    • You think the string might change in future, but you know it will always be a constant within any one version. In this case, a public static readonly field is okay. Bear in mind that it’s fine to do this with strings as they’re immutable, but you shouldn’t do this with any mutable types such as arrays. (Either expose immutable collections or use a property and return a new copy each time.)

    • You think the string might change, and it could even change within the lifetime of a program… for example, “the current date, formatted”. In this case, use a public static read-only property (one with only a getter). Note that changing from a readonly field to a read-only property is a source-compatible change, but not a binary-compatible change – so if you have plumped for my second bullet but then need to change to the third, you need to recompile everything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seem to be a number of different ways in which one can create
There doesn't seem to be many choices out there for running functional tests on
IB = Interactive Brokers There seem to be two main choices SWIG Boost.Python +
When implementing the ViewModel in a Model-View-ViewModel architecture WPF application there seem to be
There seem to be very different opinions about using transactions for reading from a
There seem to be a limitation in the maximum number of clickable options in
There seem to be no property for an image to use at all, even
There seem to be many questions asked about this subject here on stackoverflow, but
There seem to be a .CFI directive after every line and also there are
When designing an distributed application in Java there seem to be a few technologies

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.