Public const string?

前端 未结 4 1339
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 04:48

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\         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 05:30

    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.)

提交回复
热议问题