Why is it not possible to use “Using static” feature with private enum? Is there any alternative?

一个人想着一个人 提交于 2021-02-16 20:32:27

问题


I have this class where I use a private enum. I would like to use C# 6 "Using static" feature, like the following:

using static ConsoleForSimpleTests.Foo.MyEnum;

namespace ConsoleForSimpleTests
{
    public class Foo
    {
        private enum MyEnum { I, DonT, Want, This, To, Be, Public }

        private MyEnum value;

        public void SomeMethod()
        {
            switch (value)
            {
                    case I:
                    case DonT:
                    case Want:
                    case This:
                    case To:
                    case Be:
                    case Public:
                        break;
            }
        }
    }
}

NOTE: This does not compile and I understand why, it is due to the protection level for MyEnum. If I change the access modifier to either internal or public it works. What I wonder is if this is simply not possible and, if so, why is this not possible?


回答1:


If that were possible, and you had some other class in the same file, the symbols that you imported wouldn't be visible from that class.

That would be confusing; this is probably why that doesn't work.



来源:https://stackoverflow.com/questions/45223740/why-is-it-not-possible-to-use-using-static-feature-with-private-enum-is-there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!