This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

Access specifiers in C#


In this article we are going to discuss Access specifiers in C#. There are many blogs, articles are available on the internet regarding Access specifiers but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Access specifiers use in C#.

Access specifiers Introduction

Access specifiers are keywords to specify the accessibility of a class, method, property, field. The keywords are - Public, Private, Protected, Internal, Protected Internal and PrivateProtected. access specifiers properly helps ensure encapsulation and data hiding, which are key principles of object-oriented programming.

Why use Access specifiers in C#

  1. To control the visibility of class members (the security level of each individual class and class member).
  2. The greatest benefit of using access specifiers is when someone else is using your classes. By clearly specifying, what should and what should not be touched within your objects, you can protect your object internal mechanism and integrity from being misused or damaged.
  3. With bigger classes, if you made everything public, you would also make it harder for the user of your code to work with IntelliSense, which is something that is very handy when you deal with unknown libraries.

Types of Access specifiers

C# provides five types of access specifiers.

  1. Public
  2. Private
  3. Protected
  4. Internal
  5. Protected internal
Access specifiers in C#

The below table defines the scope of Access specifiers.

Access specifiers in C#

1. Public Access Specifier

It has no limits, any members or types defined as public can be accessed within the class, assembly even outside the assembly. it is accessible for all classes.

Example

class Car
{
    public string model = "Maruti Swift";
}

class Program
{
    static void Main(string[] args)
    {
        Car myObj = new Car();
        Console.WriteLine(myObj.model);
        Console.ReadKey();
    }
}

Output

Access specifiers in C#

2. Private Access Specifier

If you declare a field with a private access modifier, it can only be accessed within the same class. If you try to access it outside the class, an error will occur.

Access specifiers in C#

If you declare a field with a private access modifier, it can only be accessed within the same class

class Car
{
    private string model = "Maruti Swift";
    static void Main(string[] args)
    {
        Car myObj = new Car();
        Console.WriteLine(myObj.model);
        Console.ReadKey();
    }
}

Output

Access specifiers in C#

3. Protected Access Specifier

It is accessible within the class and has limited scope. It is also accessible within sub class or child class, in case of inheritance.

If you try to access it outside the class, an error will occur. it is not in the same class and child class.

Access specifiers in C#

If you declare a field with a Protected access modifier, it can only be accessed within the same class and child class, in case of inheritance.

class Car
{
    protected string model = "Maruti Swift";
}
class Program : Car
{
    static void Main(string[] args)
    {
        Program myObj = new Program();
        Console.WriteLine(myObj.model);
        Console.ReadKey();
    }
}

Output

Access specifiers in C#

4. Internal Access Specifier

The internal access specifier in C# is used to limit the accessibility of types and members to the current assembly. This means that any code within the same assembly (i.e., the same compiled .exe or .dll) can access the internal members, but code in a different assembly cannot.

When to Use internal

The internal access specifier is useful when you want to hide implementation details from other assemblies but still need to expose those details within the same assembly for internal use, testing, or other purposes.

Example Usage

Here's a simple example to illustrate the use of the internal access specifier:


// File: Library.cs (in AssemblyA)
namespace MyLibrary
{
    internal class InternalClass
    {
        internal void InternalMethod()
        {
            Console.WriteLine("This is an internal method.");
        }
    }

    public class PublicClass
    {
        public void PublicMethod()
        {
            InternalClass internalClass = new InternalClass();
            internalClass.InternalMethod();
        }
    }
}

In another assembly, you won't be able to access InternalClass or InternalMethod:

// File: Program.cs (in AssemblyB)
using MyLibrary;

class Program
{
    static void Main()
    {
        PublicClass publicClass = new PublicClass();
        publicClass.PublicMethod();

        // The following lines will cause compilation errors:
        // InternalClass internalClass = new InternalClass(); // Error
        // internalClass.InternalMethod(); // Error
    }
}

5. Protected Internal Access Specifier

The protected internal access specifier in C# combines the behaviors of both protected and internal access specifiers. This means that a member with this access level is accessible from:

  1. Within the same assembly - As if it were marked with internal.
  2. Derived classes - As if it were marked with protected.

Definition

The protected internal access specifier allows a member to be accessed from any class in the same assembly, and from derived classes, even if they are in a different assembly.

Example


class Car
{
    protected internal string model = "Maruti Swift";
    protected internal void Msg(string msg)
    {
        Console.WriteLine("Hello " + msg);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Car myObj = new Car();
        myObj.Msg("Rohatash");
        Console.WriteLine(myObj.model);
        Console.ReadKey();
    }
}

Output

Access specifiers in C#
Next