Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Access Modifiers

Date- Dec 09,2023

3259

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. For example, a public class is accessible to everyone without any restrictions, while an internal class may be accessible to the assembly only.

Private Access Modifier

Objects that implement private access modifiers are accessible only inside a class or a structure. As a result, we can’t access them outside the class they are created

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 and printing it
      Console.WriteLine("CourseName: " + course1.CourseName);

      // accessing print method from Course
      course1.print();
      Console.ReadLine();
    }
  }
}
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

Objects that implement public access modifiers are accessible from everywhere in our project. Therefore, there are no accessibility restrictions.

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();
    }
  }
}
OUPUT
CourseName: C#
Topics from C#

Protected Access Modifier

The protected keyword implies that the object is accessible inside the class and in all classes that derive from that class. we are going to take a look at this example to understand the behavior of the protected members.

using System;

namespace MyApplication {

  class Course {
    protected string CourseName = "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);
      Console.ReadLine();
    }
  }
}
OUTPUT
error CS0122: `MyApplication.Course.CourseName' is inaccessible due to its protection level

Now, let's try to access the protected member from a derived class.

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 = new Program();
      
      // accessing CourseName field and printing it
      Console.WriteLine("CourseName: " + program.CourseName);
      Console.ReadLine();
    }
  }
}
OUTPUT

CourseName: C#

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

Comments

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 Join Us On Facebook
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
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page