27

Is it ok to use a class like this (design / guideline specific)? I'm using MVVM Pattern.

public static class Pages
{
    public const string Home = "Home.xaml";
    public const string View2 = "View2.xaml";
    /* a few more... */
}
SBoss
  • 8,845
  • 7
  • 28
  • 44
  • 2
    For public visibility I would prefer `static readonly` to `const` because then you are able to change the value afterwards without need to recompile. – mgronber Oct 13 '11 at 09:08
  • 3
    @mgronber: It depends on the context. In most contexts I've developed in, if you change one assembly you end up recompiling clients of that assembly anyway before you use the new version. – Jon Skeet Oct 13 '11 at 09:23

4 Answers4

43

There are significant differences between const and public static readonly, and you should consider which to use with care:

(By "client" here, I mean "code in a different assembly referring to the member.)

  • If you change the value but don't recompile clients, they will still use the original value if you use const. With public static readonly, they will see the updated value. If you recompile all clients anyway, this isn't a problem.
  • Only the const form is a compile time constant, which means it can be used in:
    • Attribute arguments
    • Switch statements
    • Optional parameter declarations

If you're happy to recompile all your clients if you ever change the value, the benefits of the second bullet point point towards using const.

Of course, I wonder whether Pages really needs to be public anyway... it sounds like something which could be internal, with internal members - at which point the downsides of const go away entirely.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

A general guideline when using const for defining constant values. Whether these constants are to be accessed outside assembly? If not then declare it as

internal static class Pages
{
    public const string Home = "Home.xaml";
    public const string View2 = "View2.xaml";
    /* a few more... */
}
AksharRoop
  • 2,263
  • 1
  • 19
  • 29
  • It's definitely worth understanding the pros and cons before giving the universal advice of "use static readonly instead of const". Consider the context where if assembly X changes, all clients will be rebuilt anyway - a fairly common context in busines. Does that change the advice you'd give? – Jon Skeet Oct 13 '11 at 10:03
  • @JonSkeet It was not something that you must use readonly! I already asked a question with an answer. I don't really think in this case public class is necessary so I suggested to use internal class. Also, I thought it is useful to know about how const and readonly works; therefore, the link for item. Anyways, I should agree with what you suggested here. :-) – AksharRoop Oct 13 '11 at 10:36
  • 2
    Well, you did basically say "If it needs to be public, use public static readonly." If someone reading this site doesn't happen to have Effective C#, they're not going to get much immediate enlightenment into the cases where actually that's *not* applicable advice even if it should be public. – Jon Skeet Oct 13 '11 at 10:38
3

From the design perspective of your question, it seems like it could get messy fast using a single static object to contain all page references. Can you not just store it in the actual page object?

class view2 {
    public const string PageName = "View2.xaml";

    ... other stuff ...
}

then call it along the lines of...

goTo(view2.PageName);
flacnut
  • 1,030
  • 4
  • 11
  • 21
0

I think this is one of the best things you can do. Some more suggestions: with strings it's perfectly fine to use consts. In case you'd want to use different types, use static readonly and then initialize in a static constructor.

For a different approach using enums, see this thread. Since what you're trying to do looks a lot like a string enum, that might be the way to go for you.

And don't forget that as long as you specify your pages in code, making changes (e.g. renaming or moving a page) will be a pain. Consider using something like resources or sitemaps. (In case you only use the class for a page list, I'd go with using C#'s strongly typed resources - they will behave in the same way as your class and you won't have to code them by hand.)

Community
  • 1
  • 1
ver
  • 888
  • 1
  • 8
  • 16