中級 › is inaccessible due to its protection level の原因と直し方 › パターン2
is inaccessible due to its protection level の原因と直し方
privateなメンバーにクラスの外部からアクセスしようとすると出るコンパイルエラーです。
1 class Account { 2 private int balance = 500; 3 public string owner = "Alice"; 4 } 5 class Program { 6 static void Main(string[] args) { 7 Account a = new Account(); 8 System.Console.WriteLine(a._______); ^ 9 } 10 }
Program.cs(8,36): error CS0122: 'Account.balance' is inaccessible due to its protection level
空欄を埋めてビルドを通す
残高のような内部状態は隠蔽し、公開が必要な情報だけpublicにするのが一般的な設計です。
次の問題