C# – Access Modifiers – private access modifier

C# Access Modifiers restrict access to the members and classes. We have discussed a high-level overview of the Access Modifiers in the Article “C# – Access Modifiers“.

private Access Modifier

The scope of private access specifiers is local. That means if you declare a member within a class as a private member; you can access the member ONLY in the class itself; NOT outside the class or the classes derived from it. The scope is COMPLETELY local.

You can not declare a class as private in a namespace, but you can declare a class as a private in another class.

private members are NOT accessible from outside of the Assembly.

If you attempt to declare a private class within the namespace; you will get below Error:

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

And private class is NOT accessible from the outside of the class where it is defined. If you attempt to create an instance of private class from another class you will get below Error:

Error 1 The type or namespace name ‘PrivateClass’ could not be found (are you missing a using directive or an assembly reference?)

Here is the sample code:

(Raju)

C# – Access Modifiers – private access modifier

Leave a Reply

Your email address will not be published.

Scroll to top