title: C# Return a private type in protected function where private type will be accessible to all child classes
body: <p>I have two classes, <code>BaseO</code> and <code>PrivateO</code>, defined in another class, <code>Overflow</code>. <code>BaseO</code> is a public class, and <code>PrivateO</code> is private. I also have a child class to <code>BaseO</code>, called <code>ChildO</code>, which is also public.</p>
  
  <p>Now, in <code>BaseO</code>, I want to define a protected function <code>protected Private0 getPrivate();</code>, which <code>ChildO1</code> can access. But this is not allowed, because <code>getPrivate</code> has a different accessibility than <code>PrivateO</code>.</p>
  
  <p>I understand why this is a problem, if someone were to inherit <code>BaseO</code> from outside <code>Overflow</code> they would need to have access to <code>PrivateO</code>, which they don't. </p>
  
  <p>I've gotten around the problem for now by using objects and casts, but I'd like to do it more correctly. Is there a way to specify that <code>BaseO</code> is not intended to be inherited from outside of <code>Overflow</code>?</p>
  
  <p><code>internal</code> is an idea, but I'd like this to work even when the code is not in a different assembly.</p>
  
  <p>I have access to .NET 4.5 and C# 5, but any newer versions would also be interesting.</p>
  
  <p>Example code:</p>
  
  <pre><code>public static class Overflow
  {
      private class PrivateO
      {
          public int foo;
      }
  
      public class BaseO
      {
          protected PrivateO GetPrivate() // inconsistent accessibility
          {
              return new PrivateO();
          }
      }
  
      public class ChildO : BaseO
      {
          private int foo;
          public ChildO()
          {
              this.foo = GetPrivate().foo; // &lt;- desired behavior
          }
      }
  }
  </code></pre>
  
  <p><strong>Edit:</strong> Note that the example code is just that, an example. The real PrivateO class is a whole lot more complex, so it's not just a matter of retrieving an int. It also returns private builder classes and other non-trivial data structures.</p>
  
  <p><strong>Edit 2:</strong> Changed <code>Overflow</code> to a static class to emphasize that it in this case is used more as a namespace than as an instantiable class.</p>