Defining Classes and Objects
Learn how to define classes and create objects in C#.
Defining Classes and Objects is a free C# Academy lesson on CoddyKit — lesson 2 of 7. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 7 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Defining Classes and Objects
Welcome to the next lesson! In this lesson, you’ll learn how to define classes and create objects in C#, which are the foundation of Object-Oriented Programming. Let’s get started!

2
What is a Class?
A class is a blueprint for creating objects. It defines the attributes (data) and methods (functions) that the objects will have.
Example: A class for a "Car" might include attributes like Color and Speed, and methods like Drive() and Stop().
3
What is an Object?
An object is an instance of a class. Once a class is defined, you can create objects from it to represent specific entities with their own data.
Key Point: Objects are the building blocks of Object-Oriented Programming.
4
Defining a Class
To define a class in C#, use the class keyword followed by the class name. Example:
Tip: Use meaningful names for your classes to represent real-world entities.
using System;
class Car {
public string Color; // Attribute
public void Drive() { // Method
Console.WriteLine("The car is driving.");
}
}
5
Creating Objects
To create an object, use the new keyword followed by the class name. Example:
Key Point: Each object can have its own unique data, even if it is created from the same class.
using System;
class Car {
public string Color; // Attribute
public void Drive() { // Method
Console.WriteLine("The car is driving.");
}
}
class Program {
static void Main() {
Car myCar = new Car(); // Create an object of the Car class
myCar.Color = "Red"; // Set the attribute
Console.WriteLine("Car color: " + myCar.Color); // Outputs: Car color: Red
myCar.Drive(); // Call the method
}
}
6
Access Modifiers
Access modifiers control the visibility of class members. Common modifiers include:
- public: Accessible from anywhere.
- private: Accessible only within the same class.
- protected: Accessible within the same class and derived classes.
Example:
using System;
class Car {
private string engine; // Private attribute
public string Color; // Public attribute
public void SetEngine(string value) { // Public method to modify private attribute
engine = value;
}
public void ShowEngine() { // Public method to access private attribute
Console.WriteLine("Engine: " + engine);
}
}
class Program {
static void Main() {
Car myCar = new Car();
myCar.Color = "Blue";
myCar.SetEngine("V8");
Console.WriteLine("Car color: " + myCar.Color);
myCar.ShowEngine(); // Outputs: Engine: V8
}
}
7
Organizing Code
Keep your class definitions separate from the main program logic for better organization and readability. Use separate files for each class in larger projects.
Tip: Use namespaces to group related classes together in larger applications.
8
Practice Challenge
Create a class called Book with attributes Title and Author, and a method called DisplayInfo() that prints the details of the book. Example:
using System;
class Book {
public string Title;
public string Author;
public void DisplayInfo() {
Console.WriteLine("Title: " + Title);
Console.WriteLine("Author: " + Author);
}
}
class Program {
static void Main() {
Book myBook = new Book();
myBook.Title = "C# Programming";
myBook.Author = "John Doe";
myBook.DisplayInfo();
}
}
9
10
Great Job!
Congratulations! You’ve learned how to define classes, create objects, and use access modifiers in C#. In the next lesson, we’ll dive deeper into properties and methods to make your classes more functional. Let’s keep coding!

Frequently asked questions
Is the “Defining Classes and Objects” lesson free?
Yes — the full text of “Defining Classes and Objects” is free to read here on the web, and the C# Academy course includes 7 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defining Classes and Objects”?
Learn how to define classes and create objects in C#. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 7, so you can start here or from the beginning and move at your own pace.
How long does the “Defining Classes and Objects” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.