关于 Enum VS 属性名称冲突的问题有this、this和this 。
我的问题不是关于命名约定,而是我想知道如何解决下面代码中演示的名称冲突:
namespace Test
{
public class Person
{
// 1)
// Gender? Gender { get; set; }
// 2)
Gender Gender { get; set; }
public Person ()
{
// 1 - Error CS1061: Type `Test.Gender?' does not contain a definition for `Male' and no extension method `Male' of type `Test.Gender?' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Test)
// 2 - OK
Gender = Gender.Male;
}
}
public enum Gender
{
Male = 1,
Female
}
}
如果我在 2) 中声明属性,Gender Gender { get; set; }
则代码编译成功,但是,如果我在 1) 中声明Gender? Gender { get; set; }
(在上面的代码中注释)我得到错误
Error CS1061: Type `Test.Gender?' does not contain a definition for `Male' and no extension method `Male' of type `Test.Gender?' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Test)
为什么会这样?