Inaccessible due to its protection level?

前端 未结 5 492
臣服心动
臣服心动 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:12

    If you want to access your integers a, b, and c from outside of the class they are instantiated in, you have to declare them as public. However a cleaner option is to use a property, such as:

    public int A {get; set;}
    public int B {get; set}
    public int C {get; set;}
    

    This sets up you up to potentially limit write access from outside classes, while still leaving the the properties open for reading, such as:

    public int A {get; private set;}
    public int B {get; private set}
    public int C {get; private set;}
    
    0 讨论(0)
  • 2020-12-21 06:13

    Your problem is that, in C#, variables (in this case, fields) are marked as private if they are not specifically marked with an access modifier.

    https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx

    Classes and structures default to internal, fields, methods, events, properties, etc (basically, all members of classes and structures) default to private.

    Changing int a to public int a (and the same for b/c) will fix it.

    Though, I recommend not doing that. Instead, make a property for each.

    public A { get { return a; } set { a = value; } }
    public B { get { return b; } set { b = value; } }
    public C { get { return c; } set { c = value; } }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-21 06:28

    Access modifiers are keywords used to specify the declared accessibility of a member or a type. This section introduces the four access modifiers:

    The following five accessibility levels can be specified using the access modifiers:

    1. public : Access is not restricted.
    2. protected : Access is limited to the containing class or types derived from the containing class.
    3. Internal : Access is limited to the current assembly.
    4. protected internal: Access is limited to the current assembly or types derived from the containing class.
    5. private : Access is limited to the containing type.

    Every members in C# are implicitly private, so in your question the a,b and c are defined as private and so you could not access to them from outside of methodTest. for more information you may need to look at this page : Access Modifiers (C# Programming Guide)

    Good Luck! :)

    0 讨论(0)
  • 2020-12-21 06:28

    This is not caused by the constructor. You get the error because the field methodTest.b is private (in C#, fields are private by default; you need to specify an explicit modifier, so something like public int b;).

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