Access Modifiers (Specifiers) in C# with Program Examples

⚡ Smart Summary

Access modifiers in C# are keywords that set the visibility of a class, property, or method, restricting which external programs can reach them. Constructors initialize field values automatically whenever an object of the class is created.

  • 🔐 Private: The private access modifier limits a member to the same class, blocking access from any external program.
  • 🌐 Public: The public access modifier exposes a property or method to any external program without restriction.
  • 🛡️ Protected: The protected access modifier shares members only with classes inherited from the current class.
  • 📦 Internal: The internal access modifier permits access within the same assembly but not from an external program.
  • 🏗️ Constructor: A constructor shares the class name, takes no return type, and initializes fields when an object is created.
  • 🤖 AI assistance: GitHub Copilot scaffolds modifiers and constructors, while ML.NET maps class properties to model features.

Access Modifiers (Specifiers) in C#

What is Access Modifier (Specifier) in C#?

Access Modifiers or Access Specifiers in C# are the keywords used to define the visibility of a class property or method. It is used when you do not want other programs to see the properties or methods of a class. Access modifiers restrict access so that other programs cannot see the properties or methods of a class.

There are 6 types of access modifiers in C#:

  • Private
  • Public
  • Protected
  • Internal
  • Protected Internal
  • Private Protected

We will learn about the main access modifiers in C# with program examples as explained below.

Private Access Modifiers in C#

When Private access modifier is attached to either a property or a method, it means that those members cannot be accessed from any external program.

Example of Private Access Modifier

Let’s take an example and see what happens when we use the private access modifier.

Let’s modify the current code in our Tutorial.cs file. In the SetTutorial method, let’s change the public keyword to private.

Private Access Modifiers

Now let’s switchover to our Program.cs file. You will notice that there is a red squiggly line under the SetTutorial method.

Since we have now declared the SetTutorial method as private in our Tutorial class, Visual Studio has detected this. It has told the user by highlighting it that now this method will not work from the Program.cs file.

Private Access Modifier

C# Public Access Modifiers

When Public access modifier is attached to either a property or a method, it means that those members can be accessed from any external program. We have already seen this in our earlier examples.

Example of Public Access Modifier

C# Public Access Modifiers

Since we have defined our methods as public in the Tutorial class, they can be accessed from the Program.cs file.

Protected Access Modifiers in C#

When Protected access modifier is attached to either a property or a method, it means that those members can be accessed only by classes inherited from the current class. This will be explained in more detail in the Inheritance class.

C# Internal Access Modifiers

When an Internal access modifier is attached to either a property or a method, those members can be accessed only by an internal program but not by an external program.

C# Constructor

C# Constructors are used to initializing the values of class fields when their corresponding objects are created. A constructor is a method which has the same name as that of the class. If a constructor is defined in a class, then it will provide the first method which is called when an object is created. Suppose if we had a class called Employee. The constructor method would also be named as Employee().

The following key things need to be noted about constructor methods

  1. The C# default access modifier for the constructor needs to be made as public.
  2. There should be no return type for the constructor method.

Example of C# Constructor

Let’s now see how we can incorporate the user of constructors in our code. We will use constructors to initialize the TutorialID and TutorialName fields to some default values when the object is created.

Step 1) The first step is to create the constructor for our Tutorial class. In this step, we add the below code to the Tutorial.cs file.

C# Constructor

Code Explanation:-

  1. We first add a new method which has the same name as that of the class. Because it is the same name as the class, C# treats this as a constructor method. So now whenever the calling method creates an object of this class, this method will be called by default.
  2. In the Tutorial constructor, we are setting the value of TutorialID to 0 and TutorialName to “Default”. So whenever an object is created, these fields will always have these default values.

Now let’s switchover to our Program.cs file and just remove the line, which calls the SetTutorial method. This is because we want just to see how the constructor works.

C# Constructor

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:-

  1. The first step is to create an object for the Tutorial class. This is done via the ‘new’ keyword.
  2. We use the GetTutorial method of the Tutorial class to get the TutorialName. This is then displayed to the console via the Console.WriteLine method.

If the above code is entered properly and the program is executed, the following output will be displayed.

Output:

C# Constructor

From the output, we can see that the constructor was indeed called and that the value of the TutorialName was set to “Default”.

Note: Here the value “default” is fetched from the constructor.

FAQs

If no modifier is specified, class and struct members default to private and top-level types to internal, staying hidden until you widen their visibility.

Protected exposes a member to the same class and any derived class, even across assemblies. Internal exposes it only to types in the same assembly.

Protected internal allows access from the same assembly or any derived class. Private protected is stricter, allowing only derived classes inside the same assembly.

C# supports default, parameterized, copy, static, and private constructors. They initialize an objectโ€™s fields, run class setup once, or clone another objectโ€™s values.

A static constructor takes no modifier or parameters. It runs once, before the first object is created or static members load, initializing static fields.

Yes. A class can define several constructors with different parameter lists. The compiler picks the one matching the arguments passed to new.

Yes. GitHub Copilot suggests modifiers, constructors, and field initializers from a class name or comment inside Visual Studio and VS Code. Review each suggested visibility level.

ML.NET maps the public properties of a C# data class to model features and labels. Public getters and setters let it read those values during training.

Summarize this post with: