Blog for Junior Developers C#/.NET

Sunday, July 14, 2024

It's time for another, this time the 4th pillar of object-oriented programming. In the previous articles on the basics of object-oriented programming, you learned what inheritance, polymorphism, and abstraction are. Today I will try to show you what encapsulation is, using an example in C#. If you want to become a junior .NET developer, you must understand these 4 concepts.

what-is-encapsulation-in-object-oriented-programming.jpg

What is encapsulation?


Encapsulation involves hiding certain data. It is often the case that when creating a class, we do not want its individual components to be changed from the outside, because such situations may lead to incorrect operation of our applications. That is, the more we share externally, the more dangerous it can be. Imagine you are writing some banking application and you have a class for a bank account. And in it, the account balance field, this field is quite sensitive and you don't want to make it available externally, you don't want, for example, someone from the outside to change the value of this field. Therefore, such an element should be hidden inside the class and cannot be changed from outside.


Encapsulation in C#


Our data should be hidden as much as possible inside the class. As little as possible should be revealed externally. There are several ways to encapsulate data in C#. These are among others:

  • access modifiers,
  • properties,
  • constant (const),
  • readonly variables.


You have probably noticed that the declaration of a field, method or class is usually preceded by a keyword, it may be:

  • private,
  • public,
  • protected,
  • internal,
  • protected internal.


These are the basic access modifiers that describe the level of access - encapsulation. If there is no explicitly provided access modifier, the default for class members (fields, methods, properties) is private, and for classes it is internal.


Public modifier


An item marked with this attribute is accessible everywhere, from anywhere inside and outside the classroom.

public class Person
{
    public string FirstName;        
}

public class Program
{
    static void Main(string[] args)
    {
        var person = new Person();
        person.FirstName = "Kazimierz";
    }
}


We can create a new Person class object, and then we can modify the FirstName field anytime and from anywhere.


Private modifier


An element marked with this modifier is only available in the same class in which it was created.

public class Person
{
    private string _pesel;
}

public class Program
{
    static void Main(string[] args)
    {
        var person = new Person();
        person._pesel = "88888888888";//Error!!!
    }
}


In the above example, we cannot refer to the _pesel field from outside the Person class. Usually, the private modifier is used to mark some implementation details, important data that should not be visible or changed from the outside.


Protected modifier


An element marked with protected is available only in the class in which it was created, as well as in derived classes. That is, classes that inherit from the class in which this element was declared.

public class Person
{
    protected string LastName;
}

public class Employee : Person
{
    public Employee()
    {
        LastName = "Szpin";
    }
}
public class Program
{
    static void Main(string[] args)
    {
        var person = new Person();
        person.LastName = "Szpin";//Error!!!
    }
}


Similarly to the private modifier, the LastName field is not visible outside the class. It can only be visible in derived classes, in the above example in the Employee class. Sometimes this modifier can be dangerous because we share some details with all inheriting classes.


Internal modifier


Availability for a given element only in the same module.

internal class Person
{
}

public class Program
{
    static void Main(string[] args)
    {
        var person = new Person();
    }
}


This means that if the Program and Person classes are in the same module, then the above code will compile and no error will be reported. However, if these 2 classes are in separate modules, you will not be able to use the Person class in the Program class and a compilation error will be reported.


Protected internal modifier


Means accessibility to everyone in a given module and to every derived class. So it is like the sum of protected and internal accesses.

public class Person
{
    protected internal int Age;
}

public class Program
{
    static void Main(string[] args)
    {
        var person = new Person();
        person.Age = 30;
    }
}


The least frequently used modifier, I don't think I've ever needed to use it in my applications, but it's worth knowing that it exists.


Properties


We usually do not create public fields, instead it is usually better to create a property that, in addition to data assignment, may contain additional information, for example additional validation of the entered data.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; private set; }

}

public class Program
{
    static void Main(string[] args)
    {
        var person = new Person();
        person.FirstName = "Jan";
        person.LastName = "Kowalski";//Error!!!
    }
}


You can also assign access modifiers in the properties. In the example above, the LastName property has a private setter, which means it can only be changed within the class.


Const modifier


The field value marked with this modifier cannot be changed while the program is running. This is a constant whose value is determined at compile time. It applies to values ​​that never change. For example, the value of PI. A field marked with the const modifier is treated as a static field.

public class Math
{
    public const double PI = 3.14159;
}

public class Program
{

    static void Main(string[] args)
    {
        Console.WriteLine(Math.PI);
    }
}



Readonly modifier


A field value marked with this modifier can be assigned both at compile time and at runtime, but only at initialization and in the constructor. Also, unlike const, it can be an instance value, not just static.

public class Person
{
    private readonly string _name;

    public Person(string name)
    {
        _name = name;
    }
}



SUMMARY:


In today's article, I tried to provide you with the most important information about data encapsulation in C#. With encapsulation, you can hide various sensitive data or implementation details inside your class. It helps you protect yourself against undesirable behavior. Remember to share as little data as possible externally. Only what is necessary. Remember the most important access modifiers. First of all, remember how the public, private, protected and internal modifiers work, because they are the most frequently used, often during interviews for the position of a junior C# programmer, you may be asked about them (more). That's it for today, we are slowly finishing the series of articles about object-oriented programming in C#, the next article will be next Tuesday. If you still have any questions about this article or would like to read about some specific material, please write me an e-mail or leave a comment.


Author of the article:
Kazimierz Szpin

KAZIMIERZ SZPIN
Software Developer C#/.NET, Freelancer. Specializes in ASP.NET Core, ASP.NET MVC, ASP.NET Web API, Blazor, WPF and Windows Forms.
Author of the blog CodeWithKazik.com

Previous article - What is Abstraction in Object Oriented Programming?
Next article - Object-Oriented Programming in C# - The Most Important Principles You Need to Know If You Want to Become a Developer
Comments (1)
Kazimierz Szpin
KAZIMIERZ SZPIN, poniedziałek, 15 lipca 2024 11:03
What do you think about this article? I'm curious about your opinion.
Dodaj komentarz

Search engine

© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik