C# Classes and Objects
Move from loose variables to domain objects that keep related state and behavior together without treating every noun as a class.
Before this lesson
- Methods
- Scope and reference behavior
- Collections and nullable values
Define a class with state and behavior
Create and use separate object instances
Recognize when a class improves a model and when it adds ceremony
The short answer
A class defines the data and operations of a reference type. An object is one runtime instance of that class, with its own state. Use a class when a concept has identity, changing state, or rules that belong with that state.
Loose variables stop showing which facts belong together
A small program can store productName, productPrice, and productStock as separate locals. With ten products, parallel variables or lists make it easy to combine one product's name with another product's price. A type can define that these facts travel together and which operations make sense for them.
Object-oriented programming is one way to organize a system around collaborating types. It is not the only way to program C#, and a class is not automatically better than a method and a few locals. Use a class when it creates a useful boundary around state and behavior.
A class is a definition; an object is a particular instance
The Product class describes which members every Product object has. new Product() creates an object and returns a reference to it. notebook and pen can both have type Product while holding references to different objects with different values.
The dot operator accesses a member through an object reference. notebook.Price reads that object's Price field. TotalFor is an instance method, so inside it Price means the Price belonging to the current Product object.
using System;
class Counter
{
public int Value;
public void Increment()
{
Value++;
}
}
class Program
{
static void Main()
{
Counter pageViews = new Counter();
Counter downloads = new Counter();
pageViews.Increment();
pageViews.Increment();
downloads.Increment();
Console.WriteLine(pageViews.Value);
Console.WriteLine(downloads.Value);
}
}Expected output
2 1
Instance and static members answer different ownership questions
An instance member belongs to one object and is accessed through a reference. A static member belongs to the type itself. Math.Round is static because rounding does not require a particular Math object; account.Withdraw is an instance operation because it changes one account.
Do not make everything static to avoid creating objects. Static functions are good for operations with no object state. Objects are useful when state, identity, and behavior form a coherent concept.
| Question | Instance member | Static member |
|---|---|---|
| Whose state is used? | One object | No particular instance |
| Call form | order.CalculateTotal() | Math.Round(value) |
| Good fit | Actions on an entity | Stateless utility or type-wide fact |
Objects are references, not automatic copies
Classes are reference types. If Product second = first;, both variables initially refer to the same Product object. Changing second.Price is visible through first.Price because only one object changed. Use another new expression or an explicit copying strategy when you need an independent object.
Identity matters for entities: two customer objects may contain the same name but represent different people. Value-like data has different equality needs, which is why C# also offers records and structs. Those choices come after you can reason about class identity.
Good habits
- Name classes as domain concepts and methods as actions or questions.
- Keep Main responsible for coordinating rather than owning every rule.
- Do not create a class whose only purpose is to hold one temporary calculation with no meaningful boundary.
Quick knowledge check
Answer before you reveal.
01If two Product variables reference the same object, how many Product objects exist?
One object with two reference variables pointing to it.
02Should a calculation that uses no object state automatically be an instance method?
No. It may be a static method or belong to another focused type, depending on the domain.
Practice challenge
Now build it without copying.
Create a Book class with Title, Author, and IsBorrowed state plus Borrow and Return methods. Create two books and demonstrate that borrowing one does not change the other.
You are done when
- Two separate objects are created with new
- Borrow changes the state of the receiving object
- Output identifies each book and its status
Stretch: Have Borrow return false when the book is already borrowed, and print a helpful message in Main.
Open challenge in playgroundLesson checkpoint
One small step locks it in
Mark this lesson complete, then keep the momentum going.
Clear up the details
Frequently asked questions
What does this mean?
this refers to the current object instance. It can clarify which member is being accessed or distinguish a member from a same-named parameter.
Can a class contain another class object?
Yes. Object composition is central to modeling: an Order can contain a Customer reference and a list of OrderLine objects.
Is object-oriented programming required in C#?
C# has strong object-oriented features, but it also supports procedural and functional styles. Choose structures that make the problem easier to understand and change.