C#: Inconsistent accessibility: property type

前端 未结 4 1700
南笙
南笙 2020-12-06 04:00

What\'s wrong with

public partial class MainWindow : Window
{
    public ObservableCollection Tabs { get; set; }
    public ICollectionV         


        
相关标签:
4条回答
  • 2020-12-06 04:48

    All of the information above is completely correct and works fine. I just want to add from personal experience that if you are using TFS and getting your project from TFS, different Visual Studio versions can also generate this error.

    I entered a project with Visual Studio 2013 update 2 and synched with the TFS to get the solution. When I tried to run the project I got 80 errors. All of them were like "... less accessible than property...". Now it turns out I needed update 4. Once my Visual Studio was update I revered the changes and it worked perfectly.

    This might be useful if none of the above works and you are using TFS.

    0 讨论(0)
  • 2020-12-06 04:50

    The message is very straight-forward. It is contradicting to what you want to do. It says you have something declared as public (Tabs, in this case) but the guy who would be using it also need to know about TabViewModel which is not public. Either make both public or some consistent access specifier.

    0 讨论(0)
  • 2020-12-06 04:57

    MakeTabViewModela public type too.

    Obviously, it doesn't make sense for a public property on a public containing-type to be of a type that is not public. How could the property present itself to external assemblies?

    Your second sample works because, as a general rule, providing no accessibility modifiers means that the least applicable modifier is chosen as the default - in this case: private. Clearly, there are no consistency issues with declaring a private property of an internal (?) type.

    0 讨论(0)
  • 2020-12-06 04:59

    What's the accessibility on TabViewModel? I'm guessing it's not public.

    0 讨论(0)
提交回复
热议问题