枚举已弃用c#

Joh*_*nGa 29 .net c# enums

我有一个不推荐使用的(已过时的)函数,它返回一个枚举,我有一个返回枚举列表的新函数.

其中一个枚举值仅在不推荐使用的函数中使用,因此可以将枚举成员设置为过时(因为它不能在列表中)?

Kir*_*huk 47

你当然可以:

public enum EE
{
    A,

    [Obsolete]
    B
}
Run Code Online (Sandbox Code Playgroud)


Vol*_*ith 31

实际上,可能会生成编译器警告或编译器错误.

public enum TestEnum
{
    A,
    [Obsolete("Not in use anymore")]
    B,
    [Obsolete("Not in use anymore", true)]
    C,
}

public class Class1
{
    public void TestMethod()
    {
        TestEnum t1 = TestEnum.A; //Works just fine.
        TestEnum t2 = TestEnum.B; //Will still compile, but generates a warning.
        TestEnum t3 = TestEnum.C; //Will no longer compile. 
    }
}
Run Code Online (Sandbox Code Playgroud)

这将适用于使用[Obsolete]属性的任何位置.