Object-Oriented Programming in C#
Object-oriented programming (OOP) is a way of organizing code around objects — bundles of data and the behaviour that acts on that data. In C#, you define a class as a blueprint, then create objects (instances) from it. OOP rests on four pillars: encapsulation (hiding internal state behind methods and properties), inheritance (reusing one class from another), polymorphism (one method name behaving differently per type), and abstraction (exposing only what matters). C# is built around these ideas, so learning them well pays off across the whole .NET platform.
Classes and objects
// A class is a blueprint. 'Student' describes what every student has and does.
public class Student
{
// Properties hold the object's data.
public string Name { get; set; }
public int Marks { get; set; }
// A method is behaviour the object can perform.
public string Grade()
{
return Marks >= 75 ? "Distinction" : "Pass";
}
}
// Create objects (instances) from the blueprint.
Student s1 = new Student { Name = "Asha", Marks = 82 };
Console.WriteLine($"{s1.Name}: {s1.Grade()}"); // Asha: Distinction
Each object has its own copy of the properties. s1.Name belongs only to s1.
Encapsulation
Encapsulation means keeping data safe behind controlled access. A private field can only be changed through validated logic:
public class BankAccount
{
private decimal _balance; // private: outside code cannot touch it directly
public decimal Balance => _balance; // read-only property
public void Deposit(decimal amount)
{
if (amount <= 0)
throw new ArgumentException("Amount must be positive");
_balance += amount; // the only way to change the balance
}
}
Callers cannot set _balance to a negative value because the only path in is Deposit, which validates first.
Inheritance
Inheritance lets a class reuse and extend another. The : symbol means "inherits from":
public class Person
{
public string Name { get; set; }
public void Introduce() => Console.WriteLine($"I am {Name}");
}
// Teacher IS A Person, plus a subject.
public class Teacher : Person
{
public string Subject { get; set; }
}
Teacher t = new Teacher { Name = "Mr. Patil", Subject = "C#" };
t.Introduce(); // inherited from Person -> "I am Mr. Patil"
Console.WriteLine(t.Subject); // C#
Teacher automatically gets Name and Introduce() from Person.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesPolymorphism
Polymorphism lets a base type call the right version of a method for the actual object. Use virtual in the base class and override in the child:
public class Shape
{
public virtual double Area() => 0; // base version, can be replaced
}
public class Circle : Shape
{
public double Radius { get; set; }
public override double Area() => 3.14159 * Radius * Radius; // replaces base
}
public class Square : Shape
{
public double Side { get; set; }
public override double Area() => Side * Side;
}
// One loop, many shapes — each computes its own area.
Shape[] shapes = { new Circle { Radius = 2 }, new Square { Side = 3 } };
foreach (Shape shape in shapes)
Console.WriteLine(shape.Area()); // 12.56636, then 9
Abstraction with interfaces
An interface is a contract listing methods a class must provide, with no implementation:
public interface IPlayable
{
void Play(); // any class that implements IPlayable must define Play
}
public class Video : IPlayable
{
public void Play() => Console.WriteLine("Playing video...");
}
Interfaces let you write code against a capability rather than a concrete class.
Common mistakes
- Making fields
public. That breaks encapsulation. Expose data through properties ({ get; set; }) or methods, and validate where it matters. - Forgetting
virtual/override. To override a method, the base must mark itvirtual(orabstract) and the child must useoverride. - Confusing a class with an object. The class is the blueprint; the object is the thing you create with
new. - Overusing inheritance. Favour composition (holding an object) over deep inheritance chains when a "has-a" relationship fits better than "is-a".
FAQ
What's the difference between an abstract class and an interface? An abstract class can hold shared code and state; an interface is a pure contract. A class can implement many interfaces but inherit only one class.
Do I always need OOP? For small scripts, no. But for any real application, organizing code into classes keeps it maintainable.
Keep learning
- Hub: Learn .NET
- Previous: C# Data Types & Variables
- Next: Collections in C#
Practise OOP with real projects in Jalgaon — join the waitlist for the Infoplanet .NET course at /courses/dotnet.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Data Access with ADO.NET
Connect to and query a database from C# with ADO.NET — connections, commands, parameterized queries, and data readers — written safely with using blocks.
Introduction to ASP.NET Core
Understand ASP.NET Core — the modern, cross-platform web framework for .NET — and build your first minimal web API with routing and JSON responses.
ASP.NET: Web Forms vs MVC vs Razor Pages
Understand the difference between ASP.NET Web Forms, MVC, and Razor Pages — including why Web Forms is legacy — to pick the right model on modern .NET.
