Understanding C# Access Modifiers (Specifiers) with Practical Examples
What are Access Modifiers in C#?
Access modifiers—keywords that control the visibility of types and type members—are a cornerstone of encapsulation in C#. They determine which code can reference a class, property, or method, ensuring that only the intended parts of a program can interact with internal implementation details.
There are six access modifiers available in C#:
- Private
- Public
- Protected
- Internal
- Protected Internal
- Private Protected
In this tutorial you’ll learn:
- What access modifiers are and why they matter
- Private, Public, Protected, and Internal modifiers in depth
- How constructors initialize class fields
Private Access Modifiers
Marking a member as private restricts its usage to the containing class only. External code—including other classes within the same assembly—cannot reference a private member.
Example of Private Access Modifier
Let’s modify SetTutorial in Tutorial.cs to illustrate this rule. Change the public keyword to private:

Now switch to Program.cs. The compiler will underline the SetTutorial call with a red squiggly line, indicating that the method is inaccessible from this location.

Public Access Modifiers
Public members are visible to any code that can reference the containing type. This is the default for members that do not specify an access modifier.
Example of Public Access Modifier

Because SetTutorial is declared public, it can be called from Program.cs without any compiler errors.
Protected Access Modifiers
A protected member is accessible only within its own class and any derived classes. This level of access is ideal for creating reusable base classes while still safeguarding internal implementation.
Internal Access Modifiers
Internal members are visible to all code within the same assembly but not to code in other assemblies. This is useful for library components that should be exposed only to the library’s consumers.
Constructors in C#
Constructors initialize a new instance of a class. They share the class name, have no return type, and are invoked automatically when new is used.
- The default access for a constructor is
publicunless otherwise specified. - A constructor cannot declare a return type.
Example of a C# Constructor
We’ll demonstrate a constructor that sets default values for TutorialID and TutorialName when a Tutorial object is created.
Step 1) Add the constructor to Tutorial.cs:

Code Explanation
- The method named
Tutorialis treated as a constructor. It runs automatically when an instance is created. - The constructor assigns
0toTutorialIDand"Default"toTutorialName, ensuring consistent initial state.
In Program.cs, remove the call to SetTutorial to focus on constructor behavior:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
public int TutorialID;
public string TutorialName;
public Tutorial()
{
TutorialID = 0;
TutorialName = "Default";
}
public void SetTutorial(int pID, string pName)
{
TutorialID = pID;
TutorialName = pName;
}
public string GetTutorial()
{
return TutorialName;
}
static void Main(string[] args)
{
Tutorial pTutor = new Tutorial();
Console.WriteLine(pTutor.GetTutorial());
Console.ReadKey();
}
}
}
Code Explanation
- An instance of
Tutorialis created with thenewkeyword. - The
GetTutorialmethod retrieves the defaultTutorialName, which is then displayed viaConsole.WriteLine.
When executed, the console shows:
Output:

The output confirms that the constructor ran and initialized TutorialName to "Default".
Note: The value "Default" originates from the constructor, not from external code.
Summary
- Access modifiers define the visibility of class members.
privatehides members from all external code.publicexposes members to any referencing code.protectedrestricts members to the class and its descendants.internallimits visibility to the current assembly.- Constructors initialize fields when an object is instantiated.
C Language
- Understanding C# Access Modifiers: Public, Private, Protected, Internal, and More
- Understanding Java Access Modifiers: Types, Rules, and Practical Examples
- C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
- Mastering C++ Pointers: Concepts, Examples & Practical Applications
- C++ Functions Explained with Practical Code Examples
- Master C# Inheritance & Polymorphism: Practical Code Examples
- C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples
- Understanding Switch‑Case in C: Syntax, Examples, and Best Practices
- Master C Functions: Practical Examples of Recursion & Inline Techniques
- Java Multithreading Explained: Concepts, Lifecycle, and Practical Code Examples