Inaccessible due to its protection level?

前端 未结 5 600
臣服心动
臣服心动 2020-12-21 05:39

I\'m still quite new to coding in general, and while this simple program is only meant to be a test to learn how constructors work, I\'d still like to know why I\'m getting

5条回答
  •  难免孤独
    2020-12-21 06:24

    Even though the variables are in a public class, they must be declared as public as they are private by default.

    See: Access Modifiers

    Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default.

    It is best practice to use capitalized names and properties for public variables.

    public A { get; set; }
    

    Properties allow you to control the access of reading/writing of the member, as well as adding logic when they are read or set.

提交回复
热议问题