C# OOP Basics — Complete Beginner Guide
Learning Objectives
- Define a class and create objects
- Store data with fields
- Expose data safely with properties
- Call methods on objects
- Recognize the pillars of object-oriented design
What Is Object-Oriented Programming
Object-oriented programming, or OOP, organizes code around objects. An object bundles data and behavior that belong together. Instead of loose variables and functions, you model real things such as accounts, products, or players.
C# is a class-based language. Classes describe the blueprint; objects are instances built from that blueprint.
Defining a Class
class Student
{
string name;
int grade;
void Introduce()
{
Console.WriteLine("Hi, I am " + name);
}
}
A class contains fields for data and methods for actions.
Creating Objects
Student first = new Student();
Student second = new Student();
Console.WriteLine(first == second);
Each new keyword creates a separate object with its own copy of the fields.
Storing data on an object
In this simplified example the fields are used directly. Later lessons show why properties are preferred for public data.
class Counter
{
public int Value;
public void Increment()
{
Value++;
}
}
Counter c = new Counter();
c.Increment();
c.Increment();
Console.WriteLine(c.Value);
The object holds state, and the method changes that state.
Fields
Fields are variables inside a class. They represent the state of each object.
class Product
{
string name;
double price;
bool inStock;
}
Every Product instance has its own name, price, and inStock values.
Access modifiers in brief
| Modifier | Meaning |
|---|---|
| private | Visible inside the class |
| public | Visible everywhere |
| internal | Visible inside the same assembly |
Beginners often start with private fields and public methods that safely update them.
Properties
A property is a controlled member that can look like a field from the outside while using logic inside.
class BankAccount
{
private double balance;
public double Balance
{
get { return balance; }
set { balance = value; }
}
public void Deposit(double amount)
{
if (amount > 0)
{
balance += amount;
}
}
}
BankAccount account = new BankAccount();
account.Deposit(100);
Console.WriteLine(account.Balance);
The property named Balance exposes a safe view of the private field.
Auto implemented properties
When no extra logic is needed, write a compact property.
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Person p = new Person();
p.Name = "Nina";
p.Age = 28;
Console.WriteLine(p.Name);
Class vs Object
| Concept | Meaning |
|---|---|
| Class | Blueprint with structure and behavior |
| Object | One concrete instance in memory |
| Field | Piece of state on an object |
| Property | Controlled access point to state |
| Method | Action an object can perform |
Think of a class as a cookie cutter and objects as the cookies.
Methods on Objects
class Thermometer
{
private double reading;
public void SetReading(double value)
{
reading = value;
}
public string Describe()
{
if (reading > 30)
{
return "Hot";
}
return "Mild";
}
}
Thermometer t = new Thermometer();
t.SetReading(33);
Console.WriteLine(t.Describe());
Methods read and update internal state, then communicate results to the outside world.
Why OOP Helps
- Groups related data and logic together
- Hides fragile internals behind clear interfaces
- Makes code easier to test and reuse
- Maps naturally to real world domains
Common Mistakes
Mixing unrelated data in one giant class
Start small. Each class should represent one clear idea.
Making every field public
Direct open access bypasses validation. Prefer private fields plus properties or methods.
Confusing class names with object names
Student is the class. studentOne is an object of that class.
Key Takeaways
💡
- Classes are blueprints; objects are instances created with new
- Fields store state; methods define behavior
- Properties add controlled access over private fields
- Public and private modifiers manage visibility
- OOP keeps related data and logic together for clearer designs
Practice Questions
- What is the difference between a class and an object?
- Why are private fields often paired with public properties?
- What does the new keyword do when creating an object?
- Name the two main kinds of members typically found in a class.
- Design a simple Dog class with a name field and a Bark method, then create one object and use it.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type — Always start by categorizing the problem
- Incorrect formula application — Verify the formula applies to your specific case
- Unit errors — Always check units are consistent throughout
- Sign errors — Pay careful attention to positive and negative values
- Rounding too early — Keep full precision until the final answer
- Forgetting constraints — Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic — Build on the foundation laid here
- Next Topic — See how this concept extends to more advanced material
- Practice Problems — Additional exercises with solutions
- Video Explanation — Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions