Want to create interactive content? It’s easy in Genially!

Get started free

Access Modifiers

Adam Wingell

Created on March 9, 2025

Start designing with a free template

Discover more than 1500 professional designs like these:

Transcript

Here is a reference to all the Access modifiers in C#

Access Modifiers

Start

To navigate this module you can use the page numbers on the right hand side to access different pages of the module.

What are access modifiers?

#dfdfdf

Access modifiers control the visibilty of classes, methods and variables within a program Here is a example of all the access modifiers and what they do:

Here is an Example of how Access modifiers are used in C#:

Descriptions

public int PublicVar = 10; This is accessible from all locations.

private int PrivateVar = 20; This is only accessible in this class.

protected int ProtectedVar = 30; This is accessible in this class and subclasses within the program.

internal int InternalVar = 40; This is accessible in the same project this is used.

Example Program

We start with private string name; as a private field, this keeps the variable name private which cannot be accessed publicly. We then use two methods that are public, SetName and GetName to modify the variable of name. Remember these can access the variable name of the private field because they are within the same class as the private field. We then use Main(), which is the programs entry point to create an instance of the Person class. We then call SetName("Alice") to assign the name Alice to the person object. We then used GetName() to print the name to console.

Types of Access Modifiers:

+info

Here is a quick overview of Access Modifiers that you have learnt from this module.

Public and Private Modifiers:

+info

Internal and protected modifiers:

+info

Public and private modifiers:

  1. Public modifiers can be accessed from any location in the program.
  2. Private modifiers can only be accessed from within the same class it resides in.

Internal and protected modifiers

  1. Internal modifiers is able to be accessed from anywhere within the same project.
  2. Protected modifiers are accessible from the same class and subclasses it resides in.

Types of access modifiers

  1. Public modifier
  2. Private Modifier
  3. Protected Modifier
  4. Internal Modifer