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... */ }
You use the const keyword to declare a constant field or a constant local. Constant fields and locals aren't variables and may not be modified. Constants can be numbers, Boolean values, strings, or a null reference. Don't create a constant to represent information that you expect to change at any time.
String constants, also known as string literals, are a special type of constants which store fixed sequences of characters. A string literal is a sequence of any number of characters surrounded by double quotes: "This is a string."
Const values cannot have a value assigned in runtime. If you need to be able to assign value in runtime, pass the value to a constructor call and make the member readonly .
To define a string constant in C++, you have to include the string header library, then create the string constant using this class and the const keyword.
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.)
const
. With public static readonly
, they will see the updated value. If you recompile all clients anyway, this isn't a problem.const
form is a compile time constant, which means it can be used in: 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... */ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With