Inconsistent accessibility: property type

2020-04-30 02:35发布

What is wrong with this code ?

public ScreenManager _ScreenManager
        {
            get { return screenManager; }
            internal set { screenManager = value; }
        }
        ScreenManager screenManager;

and I get this error:

Inconsistent accessibility: field type 'ScreenSystem.ScreenManager' is less accessible than field 'ScreenSystem.Screen.ScreenManager'

标签: c#
3条回答
啃猪蹄的小仙女
2楼-- · 2020-04-30 02:49

I'm going to assume the type ScreenManager is internal while the class containing your property is public.

The compiler is saying that a publicly-accessible field (ScreenSystem.Screen.ScreenManager) is of a type (ScreenSystem.ScreenManager) that isn't publicly-accessible.

Your field should generally be private anyway. And you might be missing a public in front of your class ScreenSystem.ScreenManager (it defaults to internal IIRC).

查看更多
Ridiculous、
3楼-- · 2020-04-30 02:55

It means that you have ScreenManager set up as private or protected yet you are are trying to make a property public that relies on ScreenManager. You can't do that.

Make ScreenManager public or make _ScreenManager the same accessibility as ScreenManager.

查看更多
▲ chillily
4楼-- · 2020-04-30 03:12

Here is your scenario:

namespace ScreenSystem
{
    internal class ScreenManager
    {
        public string Test { get; set; }
    }
}

namespace ScreenSystem
{
    public class Screen
    {
        public ScreenManager Manager
        {
            get; internal set;
        }
    }
}

Compiler Output
Inconsistent accessibility: property type 'ScreenSystem.ScreenManager' is less accessible than property 'ScreenSystem.Screen.Manager'

Here are the solutions, depending on what you're trying to do:

  • Make the Screen class internal
  • Make the ScreenManager class public
  • Make the ScreenManager class public and the Screen class internal
  • Make the Screen.Manager property internal (and remove the internal set accessor)

Either one of the above will compile without errors. It really just depends on what you're trying to achieve.

查看更多
登录 后发表回答