Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A type that inherits another type cannot see private parts of that other type, it can see protected, internal and public parts. Consider the following code:</p> <pre><code>class A { // note that this field is private string PrivateString = string.Empty; // protected field protected string ProtectedString = string.Empty; } class B : A { } class Program { static void Main(string[] args) { Console.WriteLine("B Fields:"); B b = new B(); b.GetType() .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) .ToList() .ForEach(f =&gt; Console.WriteLine(f.Name)); Console.WriteLine("A Fields:"); A a = new A(); a.GetType() .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) .ToList() .ForEach(f =&gt; Console.WriteLine(f.Name)); } } </code></pre> <p>The output of this program is the following:</p> <pre><code>B Fields: ProtectedString A Fields: PrivateString ProtectedString </code></pre> <p>So, the type <code>A</code> has two fields; <code>PrivateString</code> and <code>ProtectedString</code>. Type <code>B</code> has one; <code>ProtectedString</code>, that it inherits from <code>A</code>. If you wish to "reach" <code>PrivateString</code> through the type <code>B</code>, you will need to navigate to its base type (<code>b.GetType().BaseType</code>).</p> <p>Note though, that even if the type <code>B</code> reports to have a field called <code>ProtectedString</code>, this field is still not declared in <code>B</code>; it is declared in <code>A</code>. This can be examined by adding <code>BindingFlags.DeclaredOnly</code> to the <code>GetFields</code> calls in the above sample program; <code>GetFields</code> will return no fields for <code>B</code>, and two for <code>A</code>.</p> <p>Translated to your code sample, this means that the type <code>test3</code> does not contain the fields <code>test2</code> and <code>test3</code>, since they are private to the type <code>test2</code> (the similarity of the field names and type names make that sentence somewhat confusing, I am afraid).a</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload