CubicLouve

Spring_MTの技術ブログ

クラスのメンバーのアクセスレベル

C#のクラスメンバーのアクセスレベルの確認

サンプルスクリプト

gist9174289

コンパイル結果

% mcs Classtest.cs
Classtest.cs(22,5): error CS0122: `TestA.privateHoge' is inaccessible due to its protection level
Classtest.cs(33,25): error CS0122: `TestA.privateHoge' is inaccessible due to its protection level
Classtest.cs(34,25): error CS0122: `TestA.protectedHoge' is inaccessible due to its protection level
Classtest.cs(34,25): error CS1540: Cannot access protected member `TestA.protectedHoge' via a qualifier of type `TestA'. The qualifier must be of type `TestC' or derived from it
Classtest.cs(7,17): (Location of the symbol related to previous error)
Compilation failed: 4 error(s), 0 warnings

まとめ

  • privateは同じクラス内のみアクセス可能。継承先のクラス、派生クラスではアクセスできない。外部からもアクセスできない
  • protectedは継承先のクラス、派生クラスからもアクセスできる。外部はアクセスできない。