C-Sharp Encapsulation and Access Modifiers

Hello Friends,

In this article, we will learn, What is encapsulation, read-only properties and access modifiers ?

C# provides full features of object-oriented programming (OOP), including Encapsulation, Abstraction, Inheritance, and Polymorphism. Here we will focus on first features of OOP that is Encapsulation.

Encapsulation means a logical group of related properties, methods, and other members are treated as a single unit or object.

The need of Encapsulation is to protect or prevent the code (data) from accidental corruption due to silly mistakes while coding like in JavaScript without using 'use strict'. Encapsulation provides a way to protect data from accidental corruption.

Abstraction and Encapsulation are related features in object oriented programming. Abstraction allows us, making relevant information visible and Encapsulation enables programmer, to implement the desired level of abstraction.

A). Encapsulation is implemented by using Properties.

B). Encapsulation is implemented by using Access Modifiers and Access Levels.

A). ENCAPSULATION IS IMPLEMENTED BY USING PROPERTIES.

C# has a new language feature that is Properties. Properties helps to protect a field in a class by reading and writing to it.

public class Vehicle { private int make; public int Make { get { return make; } set { make = value; } } //Short declaration public string Color { get; set; } private string model; // expression body definitions public string Model { get => model; set => model = value; } }

We can set properties values as follows.

Vehicle car = new Vehicle(); car.Color = "White"; car.Model = "SUV1OO";Read-only properties car.Make = 2019;

We can set properties even using object initializer.

Vehicle car = new Vehicle { Color = "White", Model = "SUV1OO", Make = 2019 };

We can get properties values by ClassObject.Properties as follows.

Vehicle car = new Vehicle(); car.Color = "White"; car.Model = "SUV1OO";Read-only properties car.Make = 2019; Console.WriteLine($"Vehicle Make {car.Make}\n Vehicle Model {car.Model}\nVehicle Color {car.Color}"); car = new Vehicle { Color = "White", Model = "SUV1OO", Make = 2019 }; Console.WriteLine($"Vehicle Make {car.Make}\n Vehicle Model {car.Model}\nVehicle Color {car.Color}");

You can test by copy following full Program

public class Vehicle { private int make; public int Make { get { return make; } set { make = value; } } //Auto-implemented properties public string Color { get; set; } private string model; // expression body definitions public string Model { get => model; set => model = value; } } class Program { static void Main() { Vehicle car = new Vehicle(); car.Color = "White"; car.Model = "SUV1OO"; car.Make = 2019; Console.WriteLine($"Vehicle Make {car.Make}\n Vehicle Model {car.Model}\nVehicle Color {car.Color}"); System.Console.ReadKey(); } }

OUTPUT: Vehicle Make 2019 Vehicle Model SUV1OO Vehicle Color White

Read-only Properties

We can make properties read-only by omitting setter accessor from properties as follows.

public class Vehicle { private int make; public int Make { get { return make; } } public string Color { get; } private string model; // expression body definitions public string Model { get => model; } }

Set Properties only once when object is created. By adding constructor to Vehicle class.

public Vehicle(int make, string model) { this.make = make; this.model = model; }

Accessing Read-only Properties by ClassName.PropertyName

car.Make car.Model

class Program { static void Main() { Vehicle car = new Vehicle(2019, "SUV1OO"); Console.WriteLine($"Vehicle Make {car.Make}\n Vehicle Model {car.Model}"); System.Console.ReadKey(); } }

OUTPUT Vehicle Make 2019 Vehicle Model SUV1OO

Similarly we can also create Write-only properties by omitting get accessor of properties.

public class Vehicle { private int make; public int Make { set { make = value; } } private string model; // expression body definitions public string Model { set => model = value; } }

We can't omit get accessor in Auto-implemented properties it must have get accessor

public string Color { set; }

B). ENCAPSULATION IS IMPLEMENTED BY USING ACCESS MODIFIERS AND ACCESS LEVELS.

An access specifier defines the scope and visibility of a class members.

C# supports the following access modifier:

1) public access modifier:

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

2) private access modifier:

The type or member can only be accessed by code in the same class.

3) protected access modifier:

The type or member can only be accessed by code in the same class or in a derived class.

4) internal access modifier:

The type or member can be accessed by any code in the same assembly, but not from another assembly.

5) protected internal access modifier:

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

6) private protected access modifier:

The type or member can be accessed by code in the same class or in a derived class within the base class assembly.

Access Modifiers Same Assembly Other Assembly
Declared Class (Local) Other Class Derived Class Other Class Derived Class
PRIVATE YES NO NO NO NO
PUBLIC YES YES YES YES YES
PROTECTED YES NO YES NO YES
INTERNAL YES YES YES NO NO
PROTECTED INTERNAL YES YES YES NO YES
PRIVATE PROTECTED (C# 7.2) YES NO YES NO NO

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories