Skip to content

Class and Object Concepts: The Building Blocks of Code

Friends, today I will talk about two fundamental concepts that can be called the backbone of programming: Class and Object. You know those concepts that appear everywhere, and when you ask “What is OOP?” they are the first to come to mind… Honestly, when I first started with these, these concepts seemed a bit unfamiliar to me, as if they were floating in the air. But over time, especially as we stepped into the world of C# and .NET, I realized how life-saving these two words actually are.

Now imagine, you are going to design a car. When designing this car, you first need to draw a “blueprint” or plan, right? In this plan, details like the color of the car, how many doors it has, how the engine operates, where the steering wheel will be… All these details are defined in that plan. This plan is essentially what we call a Class in our world. A Class is a template that determines what a object will look like, which features it has, and what it can do.

So, how do we turn this template into reality? This is where Object (or Instance) comes into play. We drew the plan for the car, now we produce an actual car based on that plan. This real car has its own color, license plate, engine sound. It can even honk while driving, turn on its headlights. Each of these cars we produce is an Object. That is, concrete entities produced from the Class template.

If we want to make this concept more concrete, let’s think: You have defined a “User” Class. This Class might include properties like user’s name, surname, email address. It might also have methods like “Log In” or “Change Password” (i.e., actions it can perform). Then, from this “User” Class, you can generate many user objects each with different information. For example, my user object might have the name “Ali” and email “ali@email.com”. Yours might be “Veli” with different details. These Ali and Veli are objects of our User Class.

Why is this Class and Object concept so important? Actually, it keeps our programs more organized, manageable, and reusable. You know those big projects where code integration can be overwhelming? Thanks to Class and Object logic, this complexity decreases significantly. You can use a Class you wrote in different parts of your project or even in other projects. This saves time and reduces errors. Of course, initially, these abstract concepts can seem a bit confusing; it might take some time to make these abstract concepts concrete.

Now, let’s move to the most critical part: code example. Suppose we want to create a simple `Product` Class. This product will have a name, a price, and a stock quantity. And also a `StockUpdate` method to change stock amount. You might wonder how to do this at first, but it’s actually quite simple. Folks say, “It was a simple proxy program itself,” and this is a similar simple start.

First, we define our Class. Inside, it will have properties named `Name`, `Price`, and `StockQuantity`. Then, a method called `UpdateStock` will take the new stock and update the current stock. After that, we will create concrete product objects from this Class.

Let’s see what might be a wrong approach (although correct, but less optimized):

// WRONG APPROACH (Less optimized)

public class Product {     public string Name { get; set; }     public decimal Price { get; set; }     public int StockQuantity { get; set; }
public void UpdateStock(int newStock) { this.StockQuantity = newStock; } }

This code works pretty well, yes. But think about the `record` types we often hear about in the C# and .NET world. Records offer a more practical solution especially for data carrier objects. They automatically implement methods like `Equals` and `GetHashCode`, are immutable, and make our code cleaner and more secure.

So, we can improve the above code with `record` as follows:

// RIGHT APPROACH (with record for cleaner code)

public record ProductRecord(string Name, decimal Price, int StockQuantity) {     public void UpdateStock(int newStock) {         // As Records are immutable, we can't assign directly to properties.         // Instead, we can create a new object or manage with different patterns.         // For simplicity, we show the concept here.         Console.WriteLine($"Product '{Name}' stock updated from {StockQuantity} to {newStock} (A new object will be created).") ;     } }

Here, you see we used `record`. Records generate their constructors automatically and allow us to define their properties more simply. However, pay attention: records are immutable by default. Once created, you cannot change their values directly. If you want to update a value, you actually need to create a new record object. This is a fantastic feature for data integrity. I may not remember exactly, but because of this, they are very useful especially in data models.

Now, let’s see how to create an object from our `ProductRecord`:

// Creating and using an object

var product1 = new ProductRecord("Laptop", 15000.50m, 50); var product2 = new ProductRecord("Mouse", 250.75m, 100);

Then, you can display product details:
Console.WriteLine($"Product: {product1.Name}, Price: {product1.Price}, Stock: {product1.StockQuantity}");
To update stock, you need to create a new object since records are immutable:
var updatedProduct1 = product1 with { StockQuantity = 45 };
And display updated stock:
Console.WriteLine($"Updated (new object) stock: {updatedProduct1.StockQuantity}");

As you see, using `record` makes the code more readable and manageable. To understand this `record` logic, some research may be necessary, as it may seem different initially from other types. Mastering this Class and Object principle in object-oriented programming provides us with incredible flexibility and power in software development. Ultimately, learning these concepts well will lead us to produce higher quality and more sustainable applications.

If you want to explore this topic more deeply, you can check Microsoft’s official documentation. Searches like about C# Classes or about C# Record types will guide you to the right places. Even on YouTube, you can find many educational videos on this topic, like this link.

Ultimately, Classes are our templates, and Objects are concrete entities produced from these templates. Understanding and using these two concepts correctly will improve your code quality. Isn’t that great? See you in the next article, take care!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.