Enum ToString with user friendly strings

后端 未结 23 2099
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 11:44

My enum consists of the following values:

private enum PublishStatusses{
    NotCompleted,
    Completed,
    Error
};

I want to be able to

23条回答
  •  眼角桃花
    2020-11-22 12:23

    Instead of using an enum use a static class.

    replace

    private enum PublishStatuses{
        NotCompleted,
        Completed,
        Error
    };
    

    with

    private static class PublishStatuses{
        public static readonly string NotCompleted = "Not Completed";
        public static readonly string Completed = "Completed";
        public static readonly string Error = "Error";
    };
    

    it will be used like this

    PublishStatuses.NotCompleted; // "Not Completed"
    

    Issue using the top "extension method" solutions:

    A private enum is often used inside another class. The extension method solution is not valid there since it must be in it's own class. This solution can be private and embedded in another class.

提交回复
热议问题