Blog for Junior Developers C#/.NET

Monday, July 15, 2024

Recently, I have received several messages from blog readers asking about what object-oriented programming is. They want to become programmers, they already know the basics of programming, but they write that it is difficult for them to understand important issues in object-oriented programming. That is, they seem to know what it is in theory, but they don't see any practical application. That's why in this short article I will introduce you to the main principles of object-oriented programming. Unfortunately, without good knowledge of this topic, you will not become a junior programmer, because your knowledge on this topic will be verified during job interviews. So we will start from the beginning, i.e. what is object-oriented programming, class, object, constructor, and finish with the 4 main mechanisms, i.e. inheritance, polymorphism, abstraction and encapsulation.

object-oriented-programming-in-csharp-the-most-important-principles-you-need-to-know-if-you-want-to-become-a-dotnet-programmer.jpg

What is object-oriented programming?


Object-Oriented programming, often called OOP, is mainly focused on objects, i.e. instances of a class, and the relationships between them. Programs are defined using objects that have different states and behaviors. Object-oriented programming makes it easier to work on code and extend applications because classes can be used repeatedly. They also have better code organization. Thanks to objects, we can modularize the code, that is, divide it into small pieces, such as blocks, that we can arrange together. Currently, the most popular object-oriented languages ​​are C#, Java, C++, Python.


Class and object


A class is a definition of an object, it describes the states and behaviors of the object. An object is an instance of a class, its behavior is defined in the class. I think it's best to understand this with an example.


C# class example


public class Employee
{
    private string _name;

    public string Position { get; set; }

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

    public void Work()
    {
        //logic
    }
}

public class Program
{    
    static void Main(string[] args)
    {
        var employee = new Employee("Jan Kowalski");
        employee.Position = "Junior Developer C#";
        employee.Work();
    }
}


In the above example, the Employee class has been defined. In the Main method of the Program class, an object was created, a new instance of this class, that is, a specific instance whose behavior was defined in the class. Behaviors, that is, methods, in our example it is the Work method. It can also have different states, i.e. fields or properties. In this case, name and position. A new employee named Jan Kowalski was also created, who works as a Junior C# Programmer, and then performs the work, i.e. the logic contained in the Work method. So a class is like a template based on which we can create new objects. Once we have a class, we can create many objects based on it.


Constructor


In the class, in addition to the _name field, the Position property and the Work method, we also have a constructor which in this case takes one parameter - the name of the employee. A constructor is a special method with the same name as the class. Does not include return type in signature. The constructor is called when the object is created. There may be many constructors if they have different signatures, or there may be none, in which case an empty constructor is launched by default.


Pillars of object-oriented programming


There are 4 pillars on which all object-oriented programming is based, they are quite important, which is why I recently wrote a dedicated article for each of them on my blog. Be sure to check them out:

  • Inheritance, a type of relationship between two classes that allows one of them to inherit the code of the other.
  • Polymorphism, the ability to save one method in different forms.
  • Abstraction, operating on objects without knowing the details of their implementation.
  • Encapsulation, i.e. hiding certain data to protect against incorrect operation.


SUMMARY:


In today's short article, I tried to summarize a series of recent articles about the 4 pillars of object-oriented programming in C#. I also introduced you to what exactly object-oriented programming is. I hope that after these articles, you now know the basic concepts often used in programming. If you have any questions or think that the topic has not been developed enough, please let me know by e-mail or in a comment. Remember that you need to know OOP-related topics if you want to become a junior .NET developer.
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 Encapsulation in Object Oriented Programming?
Next article - What is Blazor C#? Advantages of Blazor
Comments (1)
Kazimierz Szpin
KAZIMIERZ SZPIN, czwartek, 11 lipca 2024 11:02
What do you think about this article? I'm curious about your opinion.
Dodaj komentarz
© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik and Modest Programmer.