0% found this document useful (0 votes)
19 views

Access Modifiers - Learn Object-Oriented Programming in C#

This document discusses access modifiers in C# object-oriented programming. It covers the private and public access modifiers. Private members can only be accessed within the class, while public members can be accessed from anywhere. The naming conventions are private members start with an underscore and follow camelCase, while public members follow PascalCase. Examples are provided to demonstrate accessing private versus public class members.

Uploaded by

Naj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Access Modifiers - Learn Object-Oriented Programming in C#

This document discusses access modifiers in C# object-oriented programming. It covers the private and public access modifiers. Private members can only be accessed within the class, while public members can be accessed from anywhere. The naming conventions are private members start with an underscore and follow camelCase, while public members follow PascalCase. Examples are provided to demonstrate accessing private versus public class members.

Uploaded by

Naj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Back To Module Home

Object-oriented Programming
in C#

7% completed

Search Module

Introduction to Object-
Oriented Programming
in C#

Classes and Objects

Introduction to Objects and Classes


Got any feedback? Get in touch with us.
Declaration and Implementation

Accessing the Class Members

Access Modifiers

Fields

Methods

Static and Non-Static Methods

The 'this' Reference Variable


Access Modifiers
In this lesson, we will learn about the access modifiers and how to use them in our C# code.

We'll cover the following

• Types of Access Modifiers


• Private
• Example
• Naming Convention
• Public
• Example
• Naming Convention

In C#, we can impose access restrictions on different data members and member
functions. The restrictions are specified through access modifiers. Access
modifiers are tags we can associate with each member ofGot
theanyclass
feedback?
to Get in touchwhich
define with us.

of its parts can be accessed by the program/application directly.

Types of Access Modifiers


There are the following six types of access modifiers in C#:

1. private
2. public
3. protected
4. internal
5. protected internal
6. private protected

We’ll only discuss private and public for now. The rest of the access modifiers will be
discussed in the upcoming chapter on inheritance.

Private
A private modifier is used with a class member to specify that its access is limited to
the code inside of the containing class only. In other words, private members can
only be accessed by the methods of that class. A private member cannot be accessed
directly from outside the class.

If we don’t specify any access modifier with a class member, it is recognized


as private by default.

Got any feedback? Get in touch with us.


The aim is to keep it hidden from users and other classes. It is a popular practice to
keep data members private since we do not want anyone manipulating our data
directly. We can make members private by using the keyword private.

Class VendingMachine Main( )


// vendingMachine Object
private
// fields obj
private

.
// calling class members

private obj �

.
// methods

Compilation Error!
private
obj � �

Private Access Modifier

� Trying to access the private class members directly from another class

will cause a compilation error.

Example
Let’s look at an example by using the private access modifier in our code.

The following example won’t compile because we are trying to access a private
member of a class directly from another class.

1 class VendingMachine
2 {
3 //Class fields Got any feedback? Get in touch with us.
4 private int _count = 30; //Private field
5 }
6
7 class Demo //Class containing the Main() method
8 {
9 public static void Main(string[] args)
10 {
11 var vendingMachine = new VendingMachine(); //Object creation
12 //Calling the private member of the VendingMachine class
13 Console.WriteLine("The count of the products in the machine is: {0}", vendingMach
14
15 }
16 }

Run Save Reset

VendingMachine._count' is inaccessible due to its protection level, the error

message is pretty much self-explanatory and tells us that we cannot access a


private class member directly from another class.

Naming Convention
In C#, the name of a private class member should always start from an underscore _
followed by camelCase.

Public
Members with this tag can be directly accessed by anything which is in the same
Got any feedback? Get in touch with us.
scope as the class object. In other words, public members can be accessed from
anywhere in the current assembly or any other assembly that references it.

Member methods are usually public as they provide the interface through which
an application can communicate with our private members.

Public members can be declared using the keyword public.


Class VendingMachine Main( )
// vendingMachine Object
private
// fields obj
private

.
// calling class members

public

.
obj
// methods
public

obj

Public Access Modifier

Example
Let’s look at an example by using the private access modifier in our code.

1 class VendingMachine
2 {
3 //Class fields
4 private int _count = 30; //Private field
5
6 public int GetCount() //Public method
7 {
8 return _count;
9 }
Got any feedback? Get in touch with us.
10 }
11
12 class Demo //Class containing the Main() method
13 {
14 public static void Main(string[] args)
15 {
16 var vendingMachine = new VendingMachine(); //Object creation
17 //Calling the public member of the VendingMachine class
18 Console.WriteLine("The count of the products in the machine is: {0}", vendingMach
19
20 }
21 }

Run Save Reset

Public members of a class can be accessed by a class object using the . operator.


� In general, it’s not a good idea to declare public fields in a class as they
are mutable and can cause bugs in our application/program whereas
member methods are often declared public.

Naming Convention
In C#, the name of a public field should follow PascalCase.

In the next lesson, we will discuss everything about the data members or fields.

Got any feedback? Get in touch with us.

Back Mark As Completed Next

Accessing the Class Members Fields


Got any feedback? Get in touch with us.

You might also like