Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

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

In this tutorial you’ll learn:

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:

Understanding C# Access Modifiers (Specifiers) with Practical Examples

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.

Understanding C# Access Modifiers (Specifiers) with Practical Examples

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

Understanding C# Access Modifiers (Specifiers) with Practical Examples

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.

  1. The default access for a constructor is public unless otherwise specified.
  2. 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:

Understanding C# Access Modifiers (Specifiers) with Practical Examples

Code Explanation

  1. The method named Tutorial is treated as a constructor. It runs automatically when an instance is created.
  2. The constructor assigns 0 to TutorialID and "Default" to TutorialName, ensuring consistent initial state.

In Program.cs, remove the call to SetTutorial to focus on constructor behavior:

Understanding C# Access Modifiers (Specifiers) with Practical Examples

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. An instance of Tutorial is created with the new keyword.
  2. The GetTutorial method retrieves the default TutorialName, which is then displayed via Console.WriteLine.

When executed, the console shows:

Output:

Understanding C# Access Modifiers (Specifiers) with Practical Examples

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


C Language

  1. Understanding C# Access Modifiers: Public, Private, Protected, Internal, and More
  2. Understanding Java Access Modifiers: Types, Rules, and Practical Examples
  3. C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
  4. Mastering C++ Pointers: Concepts, Examples & Practical Applications
  5. C++ Functions Explained with Practical Code Examples
  6. Master C# Inheritance & Polymorphism: Practical Code Examples
  7. C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples
  8. Understanding Switch‑Case in C: Syntax, Examples, and Best Practices
  9. Master C Functions: Practical Examples of Recursion & Inline Techniques
  10. Java Multithreading Explained: Concepts, Lifecycle, and Practical Code Examples