开发者

Confusion with enums and public classes - why it does not work?

Having the following code, it keeps reporting inconsisteng accessibility but I do not understand why. It depends on the Program class being or not being public:

enum Test
    {A,B}

    public class Program  //when开发者_JAVA技巧 I remove public, it works
    {
        public Test a = Test.A;

        static void Main(string[] args)
        {

        }
    }


You haven't put an access modifier on your enum declaration, and then you are exposing it as a public field of your Program class. If you don't declare an access modifier, it is private.

public enum Test { A, B }


Because you have not explicitly declared an access modifier (ie. public, protected, internal) on your enum, it takes the default value (which is internal for classes and enums). You are then exposing that enum via a public field of your Program class, which is not allowed as the enum is not visible outside of its assembly.

You need to either declare the enum as public, or change the access modifier of the field to internal or private.

It works when you remove public from the Program class because it changes the Program class to be internal - the same as the enum. This is fine as neither of them are publicly exposed.


You can't make the property public because the enum is private. The public property would be externally public should someone use your program and the compiler tells you about it.


you probably need to set the enumeration to public

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜