如何根据命名约定在C#中命名类型为Gender的Gender 属性?

How to name a Gender property of type Gender in C# according to naming conventions?

我有一个 Gender 枚举:

public enum Gender
{
    Male,
    Female,
    Other,
    Unknown
}

我里面还有一个Gender 属性 Person class:

public Gender Gender { get; set; }

但是我得到一个错误:

The type 'Person' already contains a definition for 'Gender'

将枚举放在 class 之外确实解决了问题,但我想把它放在里面。我没有在 MSDN 中找到该特定问题的解决方案。是否有任何命名约定可以解决此类问题?

注意:我不是在征求意见,我是在问是否有针对这种情况的众所周知的命名约定。

这些案例没有 "well known naming convention"。通过在使用它的类型之外定义类型可以更好地处理这种不明确的命名(如错误消息所示)

如果类型明确地是类型的 API 表面的一部分,那么将枚举定义为成员似乎很奇怪。

没有“命名约定”可以解决您希望同一类型的两个成员共享相同名称的愿望。来自 C# 语言规范的第 10.3 节,Class 成员:

• The name of a constant, field, property, event, or type must differ from the names of all other members declared in the same class.

• The name of a method must differ from the names of all other non-methods declared in the same class. In addition, the signature (§3.6) of a method must differ from the signatures of all other methods declared in the same class,

(我的重点)

因此,您必须选择使用不同的名称,或者使用您显然已经放弃的将枚举放在 Person class 之外的选项。或者,如果你真的想封装它,也许进一步封装它:

class Person {
   class Enums {
      enum Gender {
         ...
      }
      ...
   }
   Enums.Gender Gender {get;set;}
   ...
}

虽然我觉得比较丑

如前所述,此类情况没有命名约定。但据我所知,当内部定义 class 应该经常从外部访问时,C# 指南不鼓励您使用内部类型定义。 我会说最好在定义 Person 的同一个命名空间中定义 Gender Enum,因为 Gender 是 Person 的 public 属性。