access modifiers in c# with examples

access modifiers in c# with examples
In this article [Show more]

    Access Modifiers in C#: Understanding Visibility and Encapsulation

    In C#, access modifiers control the visibility of types and their members within a program. They play a crucial role in managing the accessibility and encapsulation of your code. Let's delve into each access modifier along with examples to grasp their functionalities thoroughly.


    1. Public Access Modifier

    The public access modifier allows members to be accessed from any other code in the same assembly or from external assemblies. It provides the broadest level of accessibility.

    Example:

    public class MyClass
    {
        public int PublicMember { get; set; }
    }
    

    2. Private Access Modifier

    Members with the private access modifier can only be accessed within the same class or struct. It ensures encapsulation by restricting access to internal implementation details.

    Example:

    public class MyClass
    {
        private int PrivateMember { get; set; }
    }
    

    3. Protected Access Modifier

    The protected access modifier allows members to be accessed within the same class or within derived classes. It facilitates inheritance and polymorphism.

    Example:

    public class MyBaseClass
    {
        protected int ProtectedMember { get; set; }
    }
    
    public class MyDerivedClass : MyBaseClass
    {
        public void AccessProtectedMember()
        {
            ProtectedMember = 10; // Accessible here
        }
    }
    

    4. Internal Access Modifier

    Members marked as internal can be accessed within the same assembly but not from external assemblies. It promotes encapsulation within a project or assembly.

    Example:

    internal class InternalClass
    {
        internal int InternalMember { get; set; }
    }
    

    5. Protected Internal Access Modifier

    The protected internal access modifier allows access within the same assembly and from derived classes, combining the functionalities of both protected and internal.

    Example:

    public class MyBaseClass
    {
        protected internal int ProtectedInternalMember { get; set; }
    }
    
    public class MyDerivedClass : MyBaseClass
    {
        public void AccessProtectedInternalMember()
        {
            ProtectedInternalMember = 10; // Accessible here
        }
    }
    

    6. Private Protected Access Modifier

    Introduced in C# 7.2, private protected restricts access to derived classes within the same assembly, promoting encapsulation while allowing limited inheritance.

    Example:

    public class MyBaseClass
    {
        private protected int PrivateProtectedMember { get; set; }
    }
    
    public class MyDerivedClass : MyBaseClass
    {
        public void AccessPrivateProtectedMember()
        {
            PrivateProtectedMember = 10; // Accessible here
        }
    }
    

    Understanding these access modifiers empowers you to design secure, modular, and maintainable C# applications while adhering to best practices in encapsulation and accessibility.

    Author Information
    • Author: Ehsan Babaei

    Send Comment



    Comments