# C# Specifiers Explained: What You Need to Know

In this article, we will learn about specifiers and how to use them. They are important for managing your code securely and maintaining a strong structure in terms of architecture and code quality. Additionally, this topic is fundamental for Object-Oriented Programming (OOP).

We use specifiers to manage the accessibility of our classes and methods. There are four different types of specifiers: ***public, private, protected, internal,*** and ***protected internal.***

## Public Specifier

The public specifier is accessible at all levels of the project.

> An important point is that the public specifier can also be accessed by other assemblies when your DLL is referenced.

**Product.cs**

```csharp
namespace Specifiers;

public class Product
{
    private string? _productName;
    public string? ProductName
    {
        get { return _productName; }
        set { _productName = value; }
    }
    public string GetProductName()
    {
        return ProductName ?? "";
    }
}
```

**Program.cs**

```csharp
Product newProduct = new Product();
newProduct.ProductName = "Test Product";
newProduct.GetProductName();
```

As you can see, we can access the public methods and fields anywhere in the project.

## Private Specifier

The private specifier is accessible only within the class where it is defined.

> The private specifier cannot be accessed by other assemblies, even when your DLL is referenced.

**Product.cs**

```csharp
namespace Specifiers;

public class Product
{
    private string? _productName;
    public string? ProductName
    {
        get { return _productName; }
        set { _productName = value; }
    }
    public string GetProductName()
    {
        if (!IsProductAvailable())
        {
        return "Product is not available";
        }
        return ProductName ?? "";
    }
    
    private bool IsProductAvailable()
    {
        return false;
    }
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735130381356/7daab0ea-bc00-41a1-a77f-141de783a2e0.png align="left")

As you can see, we cannot access the private method in `Program.cs`. However, we can access the private method in `Product.cs`

### Protected Specifier

The protected specifier can be accessed within the same class and in derived classes.

> If your derived class is in another assembly, your protected methods are still accessible.

**Product.cs**

```csharp
namespace Specifiers;

public class Product
{
    private string? _productName;
    public string? ProductName
    {
        get { return _productName; }
        set { _productName = value; }
    }
    
    private decimal? _productPrice;
    public decimal? ProductPrice
    {
        get { return _productPrice; }
        set { _productPrice = value; }
    }
    
    private bool? _productStatus;
    public bool? ProductStatus
    {
        get { return _productStatus; }
        set { _productStatus = value; }
    }
    public string GetProductName()
    {
        if (!CheckProductIsAvailable())
        {
        return "Product is not available";
        }
        return ProductName ?? "";
    }
    
    private bool IsProductAvailable()
    {
        return false;
    }
    
    protected bool? GetProductStatus()
    {
        return _productStatus;
    }
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735130382965/ad679dba-188c-4a97-bad1-f754c63a1c0b.png align="left")

As you can see, we cannot access the `GetProductStatus` method in the `Program.cs` file.

However, when I make the `Product.cs` class abstract, we can access the protected methods in the derived class.

**Product.cs**

```csharp
namespace Specifiers;

public abstract class Product
{
    private string? _productName;
    public string? ProductName
    {
        get { return _productName; }
        set { _productName = value; }
    }
    
    private decimal? _productPrice;
    public decimal? ProductPrice
    {
        get { return _productPrice; }
        set { _productPrice = value; }
    }
    
    private bool? _productStatus;
    public bool? ProductStatus
    {
        get { return _productStatus; }
        set { _productStatus = value; }
    }
    public string GetProductName()
    {
        if (!IsProductAvailable())
        {
        return "Product is not available";
        }
        return ProductName ?? "";
    }
    
    private bool IsProductAvailable()
    {
        return false;
    }
    
    protected virtual bool? GetProductStatus()
    {
        return _productStatus;
    }
}
```

**Computer.cs**

```csharp
namespace Specifiers;

public class Computer : Product
{
    protected override bool? GetProductStatus()
    {
    return base.GetProductStatus();
    }
}
```

### Internal Specifier

The internal specifier can be accessed anywhere within the project.

> The internal specifier cannot be accessed by other assemblies, even when your DLL is referenced.

**Product.cs**

```csharp
namespace Specifiers;

public abstract class Product
{
    private string? _productName;
    public string? ProductName
    {
        get { return _productName; }
        set { _productName = value; }
    }
    
    private decimal? _productPrice;
    public decimal? ProductPrice
    {
        get { return _productPrice; }
        set { _productPrice = value; }
    }
    
    private bool? _productStatus;
    public bool? ProductStatus
    {
        get { return _productStatus; }
        set { _productStatus = value; }
    }
    public string GetProductName()
    {
        if (!IsProductAvailable())
        {
        return "Product is not available";
        }
        return ProductName ?? "";
    }
    
    private bool IsProductAvailable()
    {
        return false;
    }
    
    protected virtual bool? GetProductStatus()
    {
        return _productStatus;
    }
    
    internal decimal? GetProductPrice()
    {
        return ProductPrice ?? 0;
    }

}
```

**Program.cs**

```csharp
using Specifiers;

Product computer = new Computer();
computer.ProductPrice = 150;
computer.GetProductPrice();
```

### ProtectedInternal Specifier

The protectedInternal specifier is accessible at all levels within the project, as well as by other assemblies when your DLL is referenced.

> If your derived class is in another assembly, your `protectedInternal` methods are accessible.

I have tried to explain the different types of specifiers in C# and why they are needed.

> I did not include an example of the `protectedInternal` specifier because it is very similar to the `public` specifier. The only difference is that while the `public` specifier is accessible everywhere, including in other assemblies and external code, the `protectedInternal` specifier cannot be accessed by external code in other assemblies."

You can reach the code [here](https://github.com/frttnk/C-Specifiers-Explained-What-You-Need-to-Know)
