Object-Oriented Programming
Introduction to OOP concepts: classes, objects, encapsulation, and basic inheritance. This is a foundational concept in Microsoft application framework that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world .NET experience. Take your time with each section and practice the examples
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects that contain both data (attributes) and behavior (methods). C# is an object-oriented language.. This is an essential concept that every .NET developer must understand thoroughly. In professional development environments, getting this right can mean the difference between code that works reliably and code that breaks in production. The following sections break this down into clear, digestible pieces with practical examples you can try immediately
Classes and Objects
// Class definition
public class Person
{
// Fields (attributes)
public string Name;
public int Age;
public string Email;
// Constructor
public Person(string name, int age, string email)
{
Name = name;
Age = age;
Email = email;
}
// Methods (behavior)
public void Introduce()
{
Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old.");
}
public void SendEmail(string message)
{
Console.WriteLine($"Sending email to {Email}: {message}");
}
}
// Creating objects
Person person1 = new Person("John", 25, "john@email.com");
Person person2 = new Person("Jane", 30, "jane@email.com");
// Using objects
person1.Introduce();
person2.SendEmail("Hello from C#!");Encapsulation
// Encapsulation - controlling access to data
public class BankAccount
{
// Private fields (encapsulated)
private decimal balance;
private string accountNumber;
// Public properties (controlled access)
public decimal Balance
{
get { return balance; }
private set { balance = value; }
}
public string AccountNumber
{
get { return accountNumber; }
}
// Constructor
public BankAccount(string accountNumber, decimal initialBalance)
{
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// Public methods
public void Deposit(decimal amount)
{
if (amount > 0)
{
balance += amount;
Console.WriteLine($"Deposited {amount:C}. New balance: {balance:C}");
}
}
public bool Withdraw(decimal amount)
{
if (amount > 0 && amount <= balance)
{
balance -= amount;
Console.WriteLine($"Withdrew {amount:C}. New balance: {balance:C}");
return true;
}
Console.WriteLine("Insufficient funds or invalid amount.");
return false;
}
}inheritance
// Base class
public class Vehicle
{
public string Brand { get; set; }
public int Year { get; set; }
public int Speed { get; set; }
public Vehicle(string brand, int year)
{
Brand = brand;
Year = year;
Speed = 0;
}
public virtual void Start()
{
Console.WriteLine($"{Brand} vehicle started.");
}
public void Accelerate(int increment)
{
Speed += increment;
Console.WriteLine($"Speed increased to {Speed} km/h");
}
}
// Derived class
public class Car : Vehicle
{
public int Doors { get; set; }
public Car(string brand, int year, int doors) : base(brand, year)
{
Doors = doors;
}
public override void Start()
{
Console.WriteLine($"{Brand} car with {Doors} doors started.");
}
public void OpenTrunk()
{
Console.WriteLine("Trunk opened.");
}
}
// Usage
Car myCar = new Car("Toyota", 2023, 4);
myCar.Start();
myCar.Accelerate(50);
myCar.OpenTrunk();