In the world of C# and object-oriented programming (OOP), classes and objects form the backbone of software design. They allow you to model real-world entities, encapsulate data, and implement behavior in an organized and reusable way. Whether you’re building a small application or a large-scale system, understanding classes and objects is essential.
Example:
// Defining a class
public class Car
{
// Properties
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Method
public void DisplayDetails()
{
Console.WriteLine($"{Year} {Make} {Model}");
}
}
// Using the class to create objects
Car myCar = new Car
{
Make = "Toyota",
Model = "Corolla",
Year = 2022
};
myCar.DisplayDetails(); // Output: 2022 Toyota Corolla
A class typically includes:
Example:
public class Person
{
// Fields
private string _name;
// Properties
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Age { get; set; }
// Constructor
public Person(string name, int age)
{
_name = name;
Age = age;
}
// Method
public void Greet()
{
Console.WriteLine($"Hello, my name is {_name} and I am {Age} years old.");
}
}
Objects are created using the new
keyword, which allocates memory and invokes a constructor to initialize the object.
Example:
Person john = new Person("John Doe", 30);
john.Greet(); // Output: Hello, my name is John Doe and I am 30 years old.
new
keyword.Encapsulation is the process of restricting access to certain details of an object and exposing only necessary parts. This is achieved using access modifiers:
public
: Accessible from anywhere.private
: Accessible only within the same class.protected
: Accessible within the class and its subclasses.internal
: Accessible within the same assembly.Example:
public class BankAccount
{
private decimal balance;
public void Deposit(decimal amount)
{
if (amount > 0)
{
balance += amount;
}
}
public decimal GetBalance()
{
return balance;
}
}
public class Animal { public void Eat() => Console.WriteLine("Eating..."); } public class Dog : Animal { public void Bark() => Console.WriteLine("Barking..."); } Dog myDog = new Dog(); myDog.Eat(); // Output: Eating... myDog.Bark(); // Output: Barking...
public class Animal { public virtual void Speak() => Console.WriteLine("Animal sound"); } public class Cat : Animal { public override void Speak() => Console.WriteLine("Meow"); } Animal myCat = new Cat(); myCat.Speak(); // Output: Meow
public abstract class Shape { public abstract double GetArea(); } public class Circle : Shape { public double Radius { get; set; } public override double GetArea() => Math.PI * Radius * Radius; }
IDisposable
when managing resources like file streams or database connections.Classes and objects are the foundation of object-oriented programming in C#. They allow you to create structured, reusable, and scalable code. By understanding how to define classes, instantiate objects, and manage their lifecycle, you unlock the ability to model complex systems effectively. Embrace these concepts to build robust applications and take your programming skills to the next level!
In modern C# programming, working with data collections is a common task. Understanding how to…
Exception handling is a critical part of writing robust and maintainable C# applications. It allows…
One of the common questions among Docker users is whether Docker containers consume disk space.…
Sorting data is a common operation in programming, allowing you to organize information in a…
Splitting a string into an array of substrings is a common operation in C# programming,…
Starting the Docker daemon is the first step towards managing Docker containers and images on…