C# Specifiers Explained: What You Need to Know
Understanding C# Access Modifiers for Improved Code Control

Search for a command to run...
Understanding C# Access Modifiers for Improved Code Control

No comments yet. Be the first to comment.
C# Snippets offers quick and practical C# tips to help you write cleaner, more efficient code. Perfect for developers of all levels looking to boost their coding skills with easy-to-follow tricks and best practices.
Learn to handle C# errors using try-catch blocks for better app stability.
Building a Secure and Scalable Middleware Solution for Request Filtering and Data Masking in .NET Core

Step-by-Step Guide to Installing and Configuring Terraform for AWS Infrastructure

Getting Started with Infrastructure Automation via Terraform Provisioners

How to Create and Use Key Pairs for Secure EC2 Access Using the AWS Console

Fast Setup for AWS CLI Using Your Access Key and Secret Key

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.
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
namespace Specifiers;
public class Product
{
private string? _productName;
public string? ProductName
{
get { return _productName; }
set { _productName = value; }
}
public string GetProductName()
{
return ProductName ?? "";
}
}
Program.cs
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.
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
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;
}
}

As you can see, we cannot access the private method in Program.cs. However, we can access the private method in Product.cs
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
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;
}
}

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
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
namespace Specifiers;
public class Computer : Product
{
protected override bool? GetProductStatus()
{
return base.GetProductStatus();
}
}
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
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
using Specifiers;
Product computer = new Computer();
computer.ProductPrice = 150;
computer.GetProductPrice();
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
protectedInternalmethods 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
protectedInternalspecifier because it is very similar to thepublicspecifier. The only difference is that while thepublicspecifier is accessible everywhere, including in other assemblies and external code, theprotectedInternalspecifier cannot be accessed by external code in other assemblies."
You can reach the code here