Search⌘ K
AI Features

Access Modifiers

Explore how C# access modifiers control the visibility of class members. Understand public, private, protected, and protected internal keywords to manage access within and across classes and assemblies.

We'll cover the following...

public

The public keyword makes a class (including nested classes), property, method or field available to every consumer:

C#
using System;
public class Dog
{
//these public attributes of class are available in other methods and classes
public string name = "lucy";
public string gender = "female";
public int age = 5;
public int size = 5;
public bool healthy = true;
};
class PublicExample
{
static void Main()
{
Dog dogObj = new Dog(); //making object of Dog class
//the public members of class Dog can be accessed directly using dot operator
Console.WriteLine("Doggo's name is: {0}",dogObj.name);
Console.WriteLine("Doggo's gender is: {0}",dogObj.gender);
Console.WriteLine("Doggo's age is: {0}",dogObj.age);
Console.WriteLine("Doggo's size is: {0}",dogObj.size);
Console.WriteLine("Is Doggo healthy? {0}",dogObj.healthy);
}
}

private

The private keyword marks properties, methods, fields and nested classes for use ...