Enum and property naming conflicts

后端 未结 2 2009
南笙
南笙 2020-12-08 13:34

When using a class that has an enum property, one usually gets a naming conflict between the property name and the enum type. Example:

enum Day{ Monday, Tues         


        
相关标签:
2条回答
  • 2020-12-08 14:03

    There is no conflict. In fact, the .NET Framework style guide encourages you to do this, e.g. if you have a class that has a single property of a type (no matter if enum or class), then you should name it the same. Typical example is a Color property of type Color. It's fine, unless there are two colors - in that case both should add something to the name (i.e. BackColor and ForeColor, instead of Color and BackColor).

    0 讨论(0)
  • 2020-12-08 14:20

    So long as the enumeration isn't nested within MyDateClass, I don't see that that's a problem. It's far from uncommon (in my experience) to have a property with the same name as the type it returns. I'll see if I can find some examples in the framework...

    EDIT: First example: DateTimeOffset.DateTime (not an enum, but that's somewhat irrelevant)

    0 讨论(0)
提交回复
热议问题