Object-Oriented Programming (OOP) is the foundation of modern C# development. Whether you're building console apps, web applications, desktop software, or APIs, you are constantly working with classes, objects, methods, and inheritance.
This article walks step-by-step through OOP in C#:
Basic program elements (identity, variables, methods)
Classes and objects
Static vs instance members
Constructors and
thisAccess modifiers and encapsulation
Inheritance,
base, method overridingPolymorphism (overloading and overriding)
Sealed classes
Abstraction and abstract classes
Interfaces and multiple inheritance through interfaces
1. Program Elements in C#
Before OOP, you must understand what a program is made of. A program is a set of instructions that consists of:
Identity
Variables
Methods
1.1 Identity
An identity is the name given to a program component:
Program name
Class name
Variable name
Method name
These names are used to reference and access those members.
1.2 Variables – "Named Memory Locations"
A variable is a name given to a memory location used to store data.
string name = "Amar";
int age = 23;
double salary = 35000.00;
bool married = false;string,int,double,boolare data types.name,age,salary,marriedare identities for each memory location.
Variables store the state (data) of your program or objects.
1.3 Methods – "Blocks of Logic"
A method is a block of instructions with a name. It:
Takes input (parameters)
Performs operations
Returns output (optional)
int Add(int a, int b)
{
int c = a + b;
return c;
}Here:
Addis the method name.aandbare parameters.cis a local variable.return c;sends the result back.
2. Introduction to Object-Oriented Programming in C#
C# is a fully object-oriented language. OOP is about defining objects and establishing communication between them.
There are four main principles of OOP:
Encapsulation: binding data and methods together
Inheritance: reusing members of one class in another
Abstraction: hiding implementation details, exposing only necessary features
Polymorphism: one object behaving differently in different situations
We implement these principles using classes and objects.
3. Classes and Objects
3.1 Class – The Blueprint
A class is a blueprint or template. It defines data (variables) and behavior (methods).
class Account
{
long number;
double balance;
void Withdraw()
{
// logic
}
void Deposit()
{
// logic
}
}A class itself is not an object; it just describes what objects of that type look like.
3.2 Object – An Instance of a Class
An object is a runtime instance of a class. Its instance variables get memory when the object is created.
Account acc = new Account();Account→ classacc→ object (instance)
You can create multiple objects from the same class model.
4. Types of Variables in C#
In C#, variables are categorized to reflect scope and usage:
Static variables: shared across all objects
Instance variables: unique per object
Method parameters: input to methods
Local variables: temporary data inside methods
4.1 Static Variables
Declared with the
statickeyword inside a class.Shared by all instances of the class.
Accessed by using the class name.
class Bank
{
public static string BankName = "AXIS";
}Usage:
Console.WriteLine(Bank.BankName);Static variables are automatically initialized with default values based on their types (0, 0.0, false, null, etc.).
4.2 Instance Variables
Declared inside a class but without
static.Each object gets its own copy.
class Employee
{
int id; // instance variable
string name; // instance variable
}Each Employee The object has its own id and name.
4.3 Local Variables & Method Parameters
Method parameters: passed into a method and used only inside that method.
Local variables: declared inside the method body; exist only within that method.
int Add(int a, int b) // a and b are parameters
{
int c = a + b; // c is a local variable
return c;
}5. Methods in Detail
Methods in C# are classified based on:
Whether they take arguments
Whether they return a value
Examples
No arguments, no return value
With arguments, no return value
With arguments, with a return value
No arguments, with return value
C# also distinguishes between:
Static methods – accessed via class name
Instance methods – accessed via object reference
class Program
{
// Static method
static void Fun()
{
Console.WriteLine("Static Fun");
}
// Instance method
void Show()
{
Console.WriteLine("Instance Show");
}
static void Main()
{
Program.Fun(); // static
Program p = new Program();
p.Show(); // instance
}
}6. Constructors and Object Creation
6.1 Constructor Basics
A constructor is a special method that:
Has the same name as the class
Has no return type
Runs automatically when an object is created
class Employee
{
public Employee()
{
Console.WriteLine("Object created");
}
}
class Program
{
static void Main()
{
Employee emp = new Employee(); // calls constructor
}
}6.2 Instance Methods
Any non-static method is an instance method and must be called on an object:
class Program
{
public static void Main()
{
Program obj = new Program();
obj.Fun();
}
void Fun()
{
Console.WriteLine("fun");
}
}7. this Keyword and Parameterized Constructors
7.1 What is this?
this is a reference to the current object. It's used inside instance methods and constructors to:
Refer to instance variables
Avoid naming conflicts between parameters and fields
class Program
{
int a;
Program(int a)
{
this.a = a; // left: instance, right: parameter
}
}7.2 Parameterized Constructors
A constructor with parameters is called a parameterized constructor. It helps set initial values during object creation.
class Employee
{
int id;
string name;
public Employee(int id, string name)
{
this.id = id;
this.name = name;
}
public void Details()
{
Console.WriteLine($"{id}, {name}");
}
}
class Program
{
static void Main()
{
Employee e1 = new Employee(101, "Amar");
Employee e2 = new Employee(102, "Annie");
e1.Details();
e2.Details();
}
}
Join the conversation! Your thoughts help the community grow.