[C#] Access Modifiers

Access Modifiers (private, protected, public …) can be applied to types (classes, interfaces, structures, enumerations, and delegates) or their members (fields, properties, methods, constructors …)   

 

1. Access Modifiers to Types

  • public: no access restrictions
  • internal: accessible only within the current assembly

 

2. Access Modifiers to Nested Types

  • public: no access restrictions
  • private: accessible within the defining class or struct
  • protected: accessible within the defining class or by derived classes
  • internal: can be accessible only within the current assembly
  • protected internal: accessible within the defining assembly, the defining class, or by derived classes.

 

3. Access Modifiers to Members

  • public: no access restrictions
  • private: accessible within the defining class or struct
  • protected: accessible within the defining class or by derived classes
  • internal: accessible only within the current assembly
  • protected internal: accessible within the defining assembly, the defining class, or by derived classes.

 

4. Default Modifiers

  • for types: internal
  • for members: private
class Car
{
    Car() { }
}

The class “Car” is implicitly set to “internal“, and the constructor is set to “private“. So you cannot create an instance of the class unless you change the access modifier to “public” or provide the “public” static method to return an instance of the class.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s