C# OOP

C# Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects.

C# is an object-oriented programming language, which means that everything in C# is associated with classes and objects, along with their attributes and methods.


Procedural vs. Object-Oriented Programming

Procedural programming is about writing procedures or methods that perform operations on the data.

Object-Oriented programming, on the other hand, is about creating objects that contain both data and methods. OOP provides a clear structure for the programs and helps keep C# code DRY (Don't Repeat Yourself).

What is DRY?

The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating them.


Advantages of OOP

Object-Oriented Programming has several major advantages over procedural programming:

  1. Faster to Execute: OOP is faster and easier to execute because the data and functions are located together.
  2. Clear Structure: It provides a clear and understandable structure for complex programs.
  3. Code Reusability: It helps to keep the C# code DRY, which makes the code easier to maintain, modify, and debug.
  4. Less Code: It makes it possible to create fully reusable applications with less code and shorter development time.

Classes and Objects Analogy

To understand OOP, let's look at a real-life analogy.

Analogy 1: Fruits

The Fruit class has attributes like color and taste. When we create an object like Apple, it inherits these attributes from the class, but we can give it specific values (e.g., color = Red, taste = Sweet).

Analogy 2: Cars

The Car class has properties like weight and color, and methods like Drive() and Brake(). Each specific car object will have its own weight and color, but they all share the same driving and braking behaviors defined in the Car class.


The 4 Pillars of OOP

In the next few lessons, we will dive deep into the core concepts (the four pillars) of Object-Oriented Programming in C#:

  1. Encapsulation: Hiding the internal state and requiring all interaction to be performed through an object's methods (Properties).
  2. Inheritance: Creating new classes based on existing classes to promote code reuse.
  3. Polymorphism: Allowing methods to do different things based on the object it is acting upon.
  4. Abstraction: Hiding complex implementation details and showing only the essential features of the object.

Pro Tip: Grasping OOP concepts is the most important step in becoming a proficient C# developer. Take your time with the upcoming lessons!


Exercise

?

What does the acronym DRY stand for in programming?