Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C#
  4. Complete Guide to Access Modifiers in C# with Examples

Complete Guide to Access Modifiers in C# with Examples

Date- Dec 09,2023 Updated Mar 2026 3309
csharp access modifiers

Access Modifiers

Access modifiers in C# are used to specify the scope of accessibility of a member of a class or type of the class itself. They play a vital role in implementing encapsulation, one of the core principles of object-oriented programming. By controlling how class members are accessed, developers can prevent unauthorized access and modification of data, thus enhancing the security and integrity of applications. In this guide, we will explore various access modifiers in C#, their uses, and provide practical examples.

Private Access Modifier

The private access modifier restricts access to the containing class or structure only. Members declared as private cannot be accessed from outside the class they are defined in. This is useful for hiding implementation details and exposing only what is necessary through public methods.

using System;
namespace MyApplication {
    class Course {
        private string CourseName = "C#";
        private void Print() {
            Console.WriteLine("Topics from C#");
        }
    }
    class Program {
        static void Main(string[] args) {
            // creating object of Course class
            Course course1 = new Course();
            // accessing CourseName field will cause an error
            // Console.WriteLine("CourseName: " + course1.CourseName);
            // accessing print method will also cause an error
            // course1.Print();
            Console.ReadLine();
        }
    }
}

Attempting to access the private members of the Course class from outside will result in a compilation error, as demonstrated below:

// OUTPUT:
// error CS0122: 'MyApplication.Course.CourseName' is inaccessible due to its protection level
// error CS0122: 'MyApplication.Course.Print()' is inaccessible due to its protection level

Public Access Modifier

The public access modifier allows members to be accessible from anywhere in the project. This means there are no restrictions on the visibility of public members, making them ideal for APIs or any functionality that needs to be widely accessible.

using System;
namespace MyApplication {
    class Course {
        public string CourseName = "C#";
        public void Print() {
            Console.WriteLine("Topics from C#");
        }
    }
    class Program {
        static void Main(string[] args) {
            // creating object of Course class
            Course course1 = new Course();
            // accessing CourseName field and printing it
            Console.WriteLine("CourseName: " + course1.CourseName);
            // accessing print method from Course
            course1.Print();
            Console.ReadLine();
        }
    }
}

When running this code, the output will show the course name and the topics:

// OUTPUT:
// CourseName: C#
// Topics from C#

Protected Access Modifier

The protected access modifier allows members to be accessible within their own class and by derived class instances. This is particularly useful for base classes that need to expose certain members to subclasses while keeping them hidden from other classes.

using System;
namespace MyApplication {
    class Course {
        protected string CourseName = "C#";
    }
    // derived class
    class Program : Course {
        static void Main(string[] args) {
            // creating object of derived class Program
            Program program = new Program();
            // accessing CourseName field from derived class
            Console.WriteLine("CourseName: " + program.CourseName);
            Console.ReadLine();
        }
    }
}

This example demonstrates accessing a protected member:

// OUTPUT:
// CourseName: C#

Internal Access Modifier

The internal access modifier allows members to be accessible only within the same assembly. This means that if two classes are in the same project, they can access each other's internal members, but classes in different assemblies cannot.

using System;
namespace MyApplication {
    class Course {
        internal string CourseName = "C#";
    }
    class Program {
        static void Main(string[] args) {
            Course course1 = new Course();
            Console.WriteLine("CourseName: " + course1.CourseName);
            Console.ReadLine();
        }
    }
}

Using internal members can help in keeping the implementation details hidden from external assemblies while still allowing access within the same project.

Protected Internal Access Modifier

The protected internal access modifier combines the features of both protected and internal. This means that the member can be accessed from within its own assembly or from derived classes in other assemblies.

using System;
namespace MyApplication {
    class Course {
        protected internal string CourseName = "C#";
    }
    class Program : Course {
        static void Main(string[] args) {
            Program program = new Program();
            Console.WriteLine("CourseName: " + program.CourseName);
            Console.ReadLine();
        }
    }
}

In this example, the member is accessible from both the derived class and within the same assembly, showcasing the flexibility of protected internal access.

Private Protected Access Modifier

The private protected access modifier is a more restrictive version of protected internal. It allows access only to derived classes that are declared in the same assembly. This is useful for limiting access while still allowing inheritance.

using System;
namespace MyApplication {
    class Course {
        private protected string CourseName = "C#";
    }
    class Program : Course {
        static void Main(string[] args) {
            Program program = new Program();
            Console.WriteLine("CourseName: " + program.CourseName);
            Console.ReadLine();
        }
    }
}

Edge Cases & Gotchas

When working with access modifiers, developers should be aware of several edge cases and gotchas:

  • Nested Classes: Access modifiers apply differently to nested classes. For example, a private member of an outer class is still accessible to its nested classes.
  • Static Members: Static members of a class are subject to the same access modifiers as instance members. However, they do not require an instance of the class to be accessed.
  • Inheritance: Protected members can only be accessed by derived classes. If a derived class is in a different assembly, only protected internal members can be accessed.
  • Interfaces: Members of interfaces are always public, regardless of the access modifier used in the implementing class.

Performance & Best Practices

Choosing the correct access modifier has implications for maintainability and performance:

  • Use Private: When you want to encapsulate the implementation details and prevent external access, prefer private members.
  • Public for APIs: Use public access for methods and properties that are intended to be part of the public API of your classes.
  • Limit Protected: Use protected sparingly to avoid exposing too much of the class's internals to derived classes.
  • Favor Internal: When designing libraries that are not intended to be used outside of their assembly, prefer internal access to keep your API clean.

Conclusion

Understanding access modifiers in C# is essential for writing clean, maintainable, and secure code. By utilizing the appropriate access modifiers, developers can effectively manage the visibility of class members, ensuring that the implementation details are hidden while exposing only what is necessary.

  • Private members are only accessible within the same class.
  • Public members are accessible from anywhere.
  • Protected members are accessible within the class and by derived classes.
  • Internal members are accessible within the same assembly.
  • Protected internal members are accessible in derived classes and the same assembly.
  • Private protected members are accessible only in derived classes within the same assembly.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Understanding Destructors in C#: A Complete Guide with Examples
Dec 09, 2023
Mastering Type Casting in C#: A Complete Guide with Examples
Dec 09, 2023
Introduction to C# Programming: Your First Steps in Software Development
Mar 08, 2026
Get random number in asp.net C#
Dec 23, 2023
Previous in C#
Complete Guide to Lists in C#: Examples and Best Practices
Next in C#
Mastering Type Casting in C#: A Complete Guide with Examples
Buy me a pizza

Comments

On this page

🎯

Interview Prep

Ace your C# interview with curated Q&As for all levels.

View C# Interview Q&As

More in C#

  • Zoom C# Wrapper Integration 12905 views
  • Convert HTML String To Image In C# 11508 views
  • The report definition is not valid or is not supported by th… 10873 views
  • Replacing Accent Characters with Alphabet Characters in CSha… 9858 views
  • Get IP address using c# 8698 views
View all C# posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor