C# Academy · Lesson

Defining Classes and Objects

Learn how to define classes and create objects in C#.

Lesson 2 of 710 steps

Defining Classes and Objects is a free C# Academy lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 7 lessons in total.

Intro to Classes and Objects

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!

Defining Classes and Objects — illustration 1

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().

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.

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.");
    }
}

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
    }
}

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
    }
}

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.

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();
    }
}

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!

Defining Classes and Objects — illustration 10
Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

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. 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. The C# Academy course includes 7 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 7.

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.

All lessons in this course

  1. What is Object-Oriented Programming?
  2. Defining Classes and Objects
  3. Understanding Properties and Methods
  4. Constructors in C#
  5. Encapsulation and Access Modifiers
  6. Inheritance: Reusing Code
  7. Polymorphism and Method Overriding
← Back to C# Academy