Rajasekar Gunasekaran
Rajasekar Gunasekaran

Reputation: 1839

How Protected Class will behave in c#

I can understand how protected modifier will work for class members(methods and variables), but anyone please tell me how Protected class will behave.

eg:-

namespace AssemblyA  
{  
  Protected class ProClass  
  {
     int a=10,b=10;
     public int get()
     {
        return a+b;
     }
  }  
}

Can anyone please explain how the protected class will behave.

Upvotes: 1

Views: 112

Answers (2)

Habib
Habib

Reputation: 223422

You will get an error:

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503964

It will fail to compile, the way you've written it. Only nested classes can be protected - and they're accessible to any classes derived from the outer class, just like other protected members.

class Outer
{
    protected class Nested
    {
    }
}

class Derived : Outer
{
    static void Foo()
    {
        var x = new Outer.Nested(); // Valid
    }
}

class NotDerived
{
    static void Foo()
    {
        var x = new Outer.Nested(); // Invalid
    }
}

Upvotes: 7

Related Questions