Skip to main content

Access Modifiers in C#

How many access modifiers in c#

In this article, we will learn about the access modifiers in C#.

1. Public Access Modifier in C#

The class member, that is defined as a Public can be accessed by other class members that are initialized outside the class. A public member can be accessed from anywhere even outside the namespace.

public class Vehicle {
    public void Color() {
        Console.WriteLine("Vehicle color red");
    }
}

2. Private Access Modifier in C#

The private access modifier restrict the member variable or function to be called outside of the parent class. A private function or variable cannot be called outside of the same class. It hides its member variable and method from other class and methods.

public class Vehicle {

    private string Name;

    public void Color() {
        Console.WriteLine("Vehicle color red");
    }
}

In the above example, you cannot call name variable outside the class because it is declared as private.

3. Protected Access Modifier in C#

The protected access modifier hides its member variables and functions from other classes and objects. This type of variable or function can only be accessed in child class. It is very important while we implementing inheritance in c#.


public class Vehicle {

    protected string Name = "Vehicle name is Car";

    public void Color() {
        Console.WriteLine("Vehicle color red");
    }
}

class Program : Vehicle {
    public static void Main(string[] args)
    {
        Program p = new();
        Console.WriteLine(p.Name);
        Console.ReadLine();
    }
}

In above example, We first inherits Vehicle class in Program class and then we created Program class object p then we access Name variable. In order to access protected member in other class we must need to inherits that class first, then and then we can used that member in other class.

4. Internal Access Modifier in C#

The internal access modifier hides its member variables and methods from other classes and objects, that is resides in other namespace. The variable or classes that are declared with internal can be access by any member within application.


class Vehicle {

    internal string Name = "Vehicle name is Car";

    public void Color() {
        Console.WriteLine("Vehicle color red");
    }
}

class Program {
    public static void Main(string[] args)
    {
        Vehicle v = new();
        Console.WriteLine(v.Name);
        Console.ReadLine();
    }
}

5. Protected Internal Access Modifier in C#

The protected internal access modifier allows its members to be accessed in derived class, containing class and classes within same application.  

protected internal is everything that protected is, plus also anything in the same assembly can access it.


class Vehicle {

    protected internal string Name = "Vehicle name is Car";

    public void Color() {
        Console.WriteLine("Vehicle color red");
    }
}

class Program {
    public static void Main(string[] args)
    {
        Vehicle v = new();
        Console.WriteLine(v.Name);
        Console.ReadLine();
    }
}

Comments

Popular posts from this blog

Angular 14 CRUD Operation with Web API .Net 6.0

How to Perform CRUD Operation Using Angular 14 In this article, we will learn the angular crud (create, read, update, delete) tutorial with ASP.NET Core 6 web API. We will use the SQL Server database and responsive user interface for our Web app, we will use the Bootstrap 5. Let's start step by step. Step 1 - Create Database and Web API First we need to create Employee database in SQL Server and web API to communicate with database. so you can use my previous article CRUD operations in web API using net 6.0 to create web API step by step. As you can see, after creating all the required API and database, our API creation part is completed. Now we have to do the angular part like installing angular CLI, creating angular 14 project, command for building and running angular application...etc. Step 2 - Install Angular CLI Now we have to install angular CLI into our system. If you have already installed angular CLI into your system then skip this step.  To install angular CLI ope...

Send an Email via SMTP with MailKit Using .NET 6

How to Send an Email in .NET Core This tutorial show you how to send an email in .NET 6.0 using the MailKit email client library. Install MailKit via NuGet Visual Studio Package Manager Console: Install-Package MailKit How to Send an HTML Email in .NET 6.0 This code sends a simple HTML email using the Gmail SMTP service. There are instructions further below on how to use a few other popular SMTP providers - Gmail, Hotmail, Office 365. // create email message var email = new MimeMessage(); email.From.Add(MailboxAddress.Parse("from_address@example.com")); email.To.Add(MailboxAddress.Parse("to_address@example.com")); email.Subject = "Email Subject"; email.Body = new TextPart(TextFormat.Html) { Text = "<h1>Test HTML Message Body</h1>" }; // send email using var smtp = new SmtpClient(); smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls); smtp.Authenticate("[Username]", "[Password]"); smtp.Se...

React js CRUD Example Step by Step

How to build basic CRUD app with ReactJS Here's an example of building a simple CRUD application for managing a list of books. Step 1: Set up the React application To set up the React application, you'll need to have Node.js and NPM installed on your computer. Open a terminal or command prompt and create a new directory for your project. mkdir react-crud-app cd react-crud-app Next, initialize a new React application using the create-react-app CLI. npx create -react-app . Step 2: Create a list of books In this step, you'll create a list of books and display them in a table. Create a new file BookList.js in the src directory and add the following code: import React from 'react' ; const BookList = ( { books } ) => { return ( < table > < thead > < tr > < th > Title </ th > < th > Author </ th > < th > Actions </ th > </ tr > ...