Access Modifiers - Learn Object-Oriented Programming in C#
Access Modifiers - Learn Object-Oriented Programming in C#
Object-oriented Programming
in C#
7% completed
Search Module
Introduction to Object-
Oriented Programming
in C#
Access Modifiers
Fields
Methods
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.
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.
.
// calling class members
private obj �
.
// methods
�
Compilation Error!
private
obj � �
� Trying to access the private class members directly from another class
�
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 }
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.
.
// calling class members
public
.
obj
// methods
public
�
obj
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 }
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.