Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protected internal class working within class but not working outside

I was trying few things and would like to know why this is happening so.

Say, I have a class called A in namespace n and I was trying to create protected internal class B.

namespace n
{
   public class A
   {
      public A()
      {
      }
   }
   protected internal class B //throwing error
   {
   }
}

But when i try like this (B as a sub class of A), its not throwing error and its built success. Could you explain me why it is so?

namespace n
{
   public class A
   {
      public A()
      {
      }
      protected internal class B // its not throwing error
      {
      }
   }      
}

Am i missing anything theoretically? Its quite a bit confusing.

like image 621
VIRA Avatar asked Sep 12 '25 01:09

VIRA


1 Answers

A class can't be protected except when it is inside another class.

The protected keyword is only valid for members of a class. In your second example, class B happens to be that member.

Think about it:
protected means: Derived classes can access this member.
As there is no such concept as derived namespaces, the protected keyword doesn't make sense for members of a namespace.

like image 145
Daniel Hilgarth Avatar answered Sep 14 '25 16:09

Daniel Hilgarth