Access Modifiers In C#

Access Modifiers In C#

#csharpinterview #junior #oop

Access modifiers in C# are used to specify the scope of accessibility of a member of a class or type of the class itself. For example, a public class is accessible to everyone without any restrictions, while an internal class may be accessible to the assembly only.

Why to use access modifiers?

Access modifiers are an integral part of object-oriented programming. Access modifiers are used to implement encapsulation of OOP. Access modifiers allow you to define who does or who doesn’t have access to certain features.


In C# there are 6 different types of Access Modifiers.

ModifierDescriptionpublicThere are no restrictions on accessing public members.privateAccess is limited to within the class definition. This is the default access modifier type if none is formally specifiedprotectedAccess is limited to within the class definition and any class that inherits from the classinternalAccess is limited exclusively to classes defined within the current project assemblyprotected internalAccess is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.private protectedAccess is limited to the containing class or types derived from the containing class within the current assembly.

public modifier

The type or member can be accessed by any other code in the same assembly or another assembly that references it. In the following example, PublicClass is a public class, and PublicProperty is a public property. Both can be accessed from outside the class.

No alt text provided for this image

private modifier

The type or member can be accessed only by code in the same class or struct. In the following example, PrivateProperty is a private property.


Accessibility

  • Cannot be accessed by object
  • By derived classes


No alt text provided for this image
The above program will give compilation error, as access to private is not permissible. In the below figure you can see the private member num2 is not available.
No alt text provided for this image

protected modifier

The type or member can be accessed only by code in the same class, or in a class that is derived from that class. In the following example, ProtectedProperty is a protected property.


Accessibility

  • Cannot be accessed by object
  • By derived classes


No alt text provided for this image
No alt text provided for this image
In the above program we try to access protected member in main it is not available as shown in the picture below that ProtectedProperty is not listed in intellisense.


internal modifier

The type or member can be accessed by any code in the same assembly, but not from another assembly.


The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll). In other words, access is limited exclusively to classes defined within the current project assembly.

Accessibility

  • In same assembly (public)
  • Can be accessed by objects of the class
  • Can be accessed by derived classes

In other assembly (internal)

  • Cannot be accessed by object
  • Cannot be accessed by derived classes

protected internal modifier

The type or member can be accessed by any code in the assembly in which it’s declared, or from within a derived class in another assembly. It is a combination of protected and internal.


The protected internal accessibility means protected OR internal, not protected AND internal. In other words, a protected internal member is accessible from any class in the same assembly, including derived classes.

The protected internal access modifier seems to be a confusing but is a union of protected and internal in terms of providing access but not restricting. It allows:

  • Inherited types, even though they belong to a different assembly, have access to the protected internal members.
  • Types that reside in the same assembly, even if they are not derived from the type, also have access to the protected internal members.

private protected modifier

The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.


The private protected accessibility means private OR protected, not private AND protected.

In other words, a private protected member is accessible by types derived from the containing class, but only within its containing assembly.

The private protected access modifier is valid only on types declared inside a type. It cannot be used on namespace-level types.

Default access

A default access level is used if no access modifier is specified in a member declaration. The following list defines the default access modifier for certain C# types:


enum: The default and only access modifier supported is public.

class: The default access for a class is private. It may be explicitly defined using any of the access modifiers.

interface: The default and only access modifier supported is public.

struct: The default access is private with public and internal supported as well.

The default access may suffice for a given situation, but you should specify the access modifier you want to use to ensure proper application behavior.

Note: Interface and enumeration members are always public and no access modifiers are allowed.

To view or add a comment, sign in

More articles by Roman Fairushyn

  • Mastering SOLID Principles in C#/.NET

    Introduction In the ever-evolving landscape of software development, the principles guiding our design and architecture…

  • Mastering the Visitor Pattern

    Introduction Embarking on a journey through the intricate world of design patterns, the Visitor pattern stands out as a…

  • Mastering the Template Method Pattern in C#

    Introduction In the ever-evolving landscape of software development, design patterns serve as the cornerstone for…

  • The Strategy Pattern in C#

    Introduction In the labyrinthine world of software engineering, patterns are like Ariadne's thread: they guide us…

  • Mastering the State Pattern in C#/.Net

    Introduction As software engineers, we often find ourselves at the helm of complex systems, navigating through the…

  • Mastering the Observer Pattern in C#/.Net

    Introduction In the realm of software engineering, mastering design patterns is akin to acquiring a Swiss Army knife…

  • The Null Object Pattern in C#/.NET

    Introduction In the vibrant world of software engineering, mastering design patterns is akin to a martial artist honing…

  • Mastering the Memento Design Pattern in C#/.NET

    A Guide for Advanced Developers Introduction In the ever-evolving landscape of software development, the ability to…

  • Mastering the Iterator Pattern in C#/.NET

    Introduction Diving into the world of design patterns, the Iterator stands out as a foundational pillar, especially for…

  • Mastering the Iterator Pattern in C#/.NET

    A Deep Dive for Experienced Software Engineers Introduction Diving into the world of design patterns, the Iterator…

Others also viewed

Explore content categories