Public const string?
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
. Withpublic 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.
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... */
}
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);