Access Modifiers | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Access Modifiers

Date- Dec 09,2023

1385

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#

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
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 | 1210
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

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts