cSharp Articles
C# Files C# Enums C# Interfaces C# Abstraction C# polymorphism C# inheritance guide C# access modifiers c# constructors C# class members C# class objects C# method overloading C# return values c# methods C# array sorting C# arrays C# forEach loop C# strings C# user input c# data type C# variables whats C#C# is a powerful programming language that thrives on the principles of Object-Oriented Programming (OOP).Â
At the core of OOP are two fundamental concepts: classes and objects.Â
If you've ever wondered how these concepts work or how they can simplify your code, you've come to the right place.Â
Let’s explore C# classes and objects in detail.
What Are Classes?
Think of a class as a blueprint for creating objects.Â
Just like an architect creates a blueprint for a house, a class defines the structure and behavior of objects.Â
It contains properties (attributes) and methods (functions) that describe what an object can do.
For instance, consider a Car
class. This class would have properties like Color
, Make
, and Model
. It could also include methods like Drive
and Stop
. Here's a simple example in C#:
public class Car
{
public string Color { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public void Drive()
{
Console.WriteLine("The car is driving.");
}
public void Stop()
{
Console.WriteLine("The car has stopped.");
}
}
In this code, the Car
class defines three properties and two methods. Now, let’s make a car object based on this blueprint.
Creating Objects
To create an object, you instantiate a class.Â
Think of it as building a house from the blueprint. Here’s how you can create a Car
object in C#:
Car myCar = new Car();
myCar.Color = "Red";
myCar.Make = "Toyota";
myCar.Model = "Corolla";
myCar.Drive(); // Outputs: The car is driving.
In this example, myCar
is an object of the Car
class. You can set its properties and call its methods.Â
This ability to create multiple objects from the same class makes your code versatile and clean.
Properties and Methods Explained
Properties
Properties in a class help define the characteristics of an object.Â
They can be of different types, such as string, int, or even other classes. Using properties efficiently can enhance the functionality of your object.
Continuing with our Car
example, you might want to add a property to track its speed. Here’s how:
public int Speed { get; set; }
Now, you can add logic to manage the speed:
public void Accelerate(int increase)
{
Speed += increase;
Console.WriteLine("The car accelerated to " + Speed + " mph.");
}
public void Decelerate(int decrease)
{
Speed -= decrease;
Console.WriteLine("The car decelerated to " + Speed + " mph.");
}
Methods
Methods define the actions that can be performed on an object.Â
They can manipulate the properties of the class, perform calculations, or return values.Â
In our Car
class, methods like Drive
, Stop
, Accelerate
, and Decelerate
give the car functionality.
Here’s how to use these new methods:
myCar.Accelerate(20); // Outputs: The car accelerated to 20 mph.
myCar.Decelerate(10); // Outputs: The car decelerated to 10 mph.
Encapsulation
Encapsulation is a critical principle of OOP. It protects the internal state of an object by restricting direct access to its properties.Â
Instead, it exposes methods for interacting with the data.Â
This ensures the integrity of your data and hides implementation details.
You can implement encapsulation by using access modifiers like private
and public
. Here’s an example:
public class Car
{
private int speed;
public void Accelerate(int increase)
{
speed += increase;
Console.WriteLine("The car accelerated to " + speed + " mph.");
}
public void Decelerate(int decrease)
{
speed -= decrease;
Console.WriteLine("The car decelerated to " + speed + " mph.");
}
}
In this code, the speed
property is private.Â
The public methods Accelerate
and Decelerate
manage the speed
variable, maintaining control over how it’s accessed.
Inheritance: A Step Further
Another essential feature of classes in C# is inheritance.Â
This allows you to create a new class based on an existing class, inheriting its properties and methods. Imagine you want to create a SportsCar
class.Â
It can inherit from the Car
class and add more specific features:
public class SportsCar : Car
{
public bool HasTurbo { get; set; }
public void ActivateTurbo()
{
if (HasTurbo)
{
Console.WriteLine("Turbo activated!");
}
}
}
Now, your SportsCar
class has all the properties and methods of Car
but also includes functionality specific to sports cars.Â
You can create a SportsCar
object and use it just like a Car
.
SportsCar mySportsCar = new SportsCar();
mySportsCar.Color = "Blue";
mySportsCar.Make = "Ferrari";
mySportsCar.Model = "488";
mySportsCar.HasTurbo = true;
mySportsCar.ActivateTurbo(); // Outputs: Turbo activated!