c# - Inaccessible due to its protection level? -
i'm still quite new coding in general, , while simple program meant test learn how constructors work, i'd still know why i'm getting error.
using system; public class methodtest { int a; int b; int c; public methodtest(int i, int j, int k) { = i; b = j; c = k; } } public class methodobj { static void main() { methodtest obj = new methodtest(10, 20, 30); console.writeline("obj = " + obj.b); console.readkey(); } }
i'm not entirely sure why i'm getting error. problem console.writeline, states cannot access obj.b. variables seem declared within public class, why can not accessed? tried searching solution this, questions found convoluted me answer translate own understanding. appreciated!
even though variables in public class, must declared public private default.
see: access modifiers
class members, including nested classes , structs, can public, protected internal, protected, internal, or private. the access level class members , struct members, including nested classes , structs, private default.
it best practice use capitalized names , properties public variables.
public { get; set; }
properties allow control access of reading/writing of member, adding logic when read or set.
Comments
Post a Comment