What's the best C# pattern for implementing a hierarchy with an enum?

前端 未结 3 738
情深已故
情深已故 2021-02-14 17:10

I\'m implementing value types which represents a distance (or length). There\'s an enum that represents the different units of measure, eg:

public enum Distance         


        
3条回答
  •  無奈伤痛
    2021-02-14 17:27

    Generally speaking, I would go with Anton's solution. But if your implementation can't use that, and you need things to be used similar to an enum, I think this is a natural way to use the units:

    DistanceUnit.Metric.Millimeter  
    DistanceUnit.Imperial.Inch  
    

    In order to use it like that, there should be:

    public static class DistanceUnit  
    {
      public static MetricDistanceUnit Metric;
      public static ImperialDistanceUnit Imperial;
    }   
    

    Where MetricDistanceUnit is:

    public enum MetricDistanceUnit  
    {
       Millimeter, Centimeter ...
    }
    

    And ImperialDistanceUnit has the same structure..

提交回复
热议问题