- Author

- Name
- Nelson Silva
- Social
Introduction
C# is one of the main languages that adopts the object-oriented programming paradigm. This approach focuses on objects that represent real-world entities. And where do classes come in? They are the blueprint from which objects are shaped.
What are Classes?
A class is the heart of object-oriented programming. It is a model or blueprint that defines a new data type. A class combines data (through fields or properties) and behavior (through methods).
Components of a Class
- Fields and Properties: These represent the state or data of a class.
- Methods: Functions associated with a class. They define what the class can do.
What are Objects?
If a class is a blueprint, an object is the real manifestation of that blueprint. It is a specific instance of a class.
Characteristics of an Object
- State: Determined by the values of fields or properties.
- Behavior: Determined by the methods of the class.
Creating and Using Objects
Let's create an object of the ClassesAndObjects class:
using System;
namespace Base {
class Program {
static void Main(string[] args) {
var enemy1 = new ClassesAndObjects();
var enemy2 = new ClassesAndObjects();
enemy1.Attack(); // I was attacked and lost a life.
enemy1.Attack(); // I was attacked and lost a life.
enemy1.Attack(); // I was attacked and lost a life.
enemy1.CheckLife(); // I am still in combat and have 2 lives.
enemy2.CheckLife(); // I am still in combat and have 5 lives.
Console.ReadKey();
}
}
}
In the example above, enemy1 and enemy2 are objects of the ClassesAndObjects class.
Encapsulation and Access Modifiers
One of the main characteristics of object-oriented programming is encapsulation. It helps protect the internal state of an object.
The main access modifiers in C# are:
- Private: The member can only be accessed within its class.
- Public: The member can be accessed from anywhere.
- Protected: The member can only be accessed within its class and by derived classes.
Conclusion
Object-oriented programming is a central pillar of the C# language. Understanding classes and objects, as well as the interaction between them, is fundamental for any C# developer. By mastering these concepts, you will be well positioned to create efficient and effective applications in C#.