Click here to Skip to main content
14,733,527 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
These access modifiers are only for member functions and data member of the class or we can apply them at class level also in c# ?

If yes then,
-Here i have tried to impliment private AccessModifiers at class level. Eventhough i have used private access modifier at class level, i'm able to access protected member functions of the parent class in child class. But private means we shouldnot be able to access that class right?(please correct me if i'm wrong).


ParentA.cs
-----------------
namespace PrivateAccessModifier
{
    private class ParentA
    {
        private int a;
        protected void methoda()
        {
            Console.WriteLine("in Parent");
        }
    }
}


ChildA.cs
----------------
namespace PrivateAccessModifier
{
    private class childA : ParentA
    {
        public void method1()
        {
            methoda();
        }
    }
}
Posted
Comments
   
Why not reading language and .NET manual instead of asking? Everything is described in detail. Also, it is intuitively clear: you can access the member and the class/structure if you have sufficient access to both. Isn't this logical?
--SA

so first things is that, if ur namespace is same need not declare the class as Private, Protected or Protected internal. because The default for non-nested types is internal. The default for nested types is private. In both cases the default (for classes) is unsealed.
Second is that, if u declare parent class member is as a Protected u will always access these member in child class.
Refer - Access Modifiers (C# Programming Guide)[^]
   
v2
just one thing to note here both the classes i.e, ParentA and childA are part of the same namespace so they will be available to be referenced by one another. you may check private classes inside namespaces [closed][^]

Regards
Pawan
   
v2
Comments
   
Namespaces do not affect access, whatsoever!
—SA
   
Namespaces do not contain anything at all. They simply define the full names of types. And the names does not effect access, do they? Use full names of types; and you will see no boundaries between assemblies. Simple as it is.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900