2

If I want to define a set of global strings in my C# program, I've been using settings so that I access them with

    ... Properties.Settings.Default.StringA ...
    ... Properties.Settings.Default.StringB ...

etc.

I was reading some SO questions that landed me with the idea I should create a static class

    public static class Marker
    {
         public static readonly string StringA = "my text A";
         public static readonly string StringB = "my_other_text";
    }

of course, accessing these would be simple with

      Marker.StringA

While I'm developing my hadbits, which method should I use for what constants or is this a personal style choice?

Thanks.

Sisyphus
  • 679
  • 6
  • 21
  • 4
    Are these meant to be user visible text? If so, you should be using a resource manager... – Jon Skeet Jan 24 '13 at 12:01
  • http://stackoverflow.com/questions/1724025/in-c-whats-the-best-way-to-store-a-group-of-constants-that-my-program-uses – Habib Jan 24 '13 at 12:02
  • @Habib - thanks! Clearly, I wasn't asking the question the right way in the search ! – Sisyphus Jan 24 '13 at 12:05
  • @Habib - In your reading of Q 1724025 that you referenced, is the consensus that if your program is a small standalone and not part of a larger project, that static classes are fine? – Sisyphus Jan 24 '13 at 12:12
  • One thing about static classes is that if the strings change, you will have to recompile and consequently retest the class. With your properties, they can be changed in a configuration file without recompiling the code. – Chris Dunaway Jan 24 '13 at 17:12

2 Answers2

0

Since all configuration files are cached by default this is just a matter of personal preference.

Is reading app.config expensive?

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0
  1. Use the resource manager for access to culture-specific resources at run time. Etc text displayed on UI.

  2. Use the App.Config or Web.Config for environment based constants. Here is a good example from Skeet https://stackoverflow.com/a/1431754/208565

  3. Use constants and readonly fields for code level usage.

Community
  • 1
  • 1
Jonathan
  • 2,318
  • 7
  • 25
  • 44