Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 13 Beginner

Classes and Objects in C#

Updated: May 17, 2026
5 min read

# CHAPTER 13

Classes and Objects

1. Introduction

In the previous chapter, we learned that a Class is a blueprint, and an Object is the physical thing built from it. Now, we will write the code to create them. We will also learn about Access Modifiers, which are the security guards of your data.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define a custom Class.
  • Use the new keyword to instantiate an Object.
  • Understand Access Modifiers (public, private).
  • Differentiate between Fields and Properties.
  • Create Methods inside a class.

3. Creating a Class

To create a blueprint, use the class keyword. Inside the class, we define Fields (variables belonging to the class) and Methods (functions belonging to the class).
csharp
12345678910111213
// The Blueprint
class Car
{
    // Fields
    public string brand;
    public string color;

    // Method
    public void Drive()
    {
        Console.WriteLine($"The {color} {brand} is driving!");
    }
}

4. Creating an Object

Once the class is defined, you can create multiple Objects (Instances) in your Main method using the new keyword.
csharp
1234567891011121314151617
class Program
{
    static void Main()
    {
        // Object 1
        Car myCar = new Car();
        myCar.brand = "Toyota";
        myCar.color = "Red";
        myCar.Drive(); // "The Red Toyota is driving!"

        // Object 2
        Car friendCar = new Car();
        friendCar.brand = "BMW";
        friendCar.color = "Black";
        friendCar.Drive(); // "The Black BMW is driving!"
    }
}

5. Access Modifiers (public vs private)

In the example above, we used the public keyword.
  • public: Any other code can access and change this variable.
  • private: ONLY the code inside the class itself can see or change this variable. It is hidden from the outside world.

If you don't write an access modifier, C# defaults variables to private.

csharp
1234567891011121314151617181920
class BankAccount
{
    private decimal balance = 1000; // Hidden!

    public void CheckBalance()
    {
        // The method is inside the class, so it can read the private variable
        Console.WriteLine($"Balance: ${balance}"); 
    }
}

class Program
{
    static void Main()
    {
        BankAccount myAccount = new BankAccount();
        // myAccount.balance = 0; // ERROR! Cannot access private variable
        myAccount.CheckBalance(); // OK! The method is public.
    }
}

*This is the core of Encapsulation!*

6. Fields vs. Properties

In modern C#, making fields public is considered bad practice because anyone can change them without validation. C# introduced Properties (get and set), which look like variables but act like methods behind the scenes.
csharp
123456789101112131415161718
class Employee
{
    // A modern C# Auto-Implemented Property
    public string Name { get; set; }
    
    // A Property with custom logic
    private int _age; // Private backing field (conventionally starts with underscore)
    
    public int Age
    {
        get { return _age; }
        set 
        { 
            if (value >= 18) { _age = value; } // Validation!
            else { Console.WriteLine("Invalid Age!"); }
        }
    }
}

7. Mini Project: Employee Management System

csharp
1234567891011121314151617181920212223242526272829303132
using System;

namespace EnterpriseApp
{
    class Employee
    {
        public string Name { get; set; }
        public string Department { get; set; }

        public void DisplayDetails()
        {
            Console.WriteLine($"Employee: {Name} | Dept: {Department}");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee emp1 = new Employee();
            emp1.Name = "Alice";
            emp1.Department = "IT";

            Employee emp2 = new Employee();
            emp2.Name = "Bob";
            emp2.Department = "HR";

            emp1.DisplayDetails();
            emp2.DisplayDetails();
        }
    }
}

8. OOP and Memory Explanation

When you write Car c1 = new Car();, the new keyword tells the CLR: "Go to the Heap, carve out a block of memory large enough to hold all the variables defined in the Car class, and return the memory address." The variable c1 on the Stack simply holds that memory address (Reference).

9. Common Mistakes

  • Forgetting new: Car c1; c1.Drive(); will crash with a NullReferenceException. You declared the variable, but you never built the actual object!
  • Using public fields: Don't write public int health;. Always use properties: public int Health { get; set; }.

10. Best Practices

  • Properties should use PascalCase (Name), while private backing fields should use camelCase with an underscore (_name).
  • One Class per File: In real projects, you don't put all classes in Program.cs. You create a new file (e.g., Car.cs) for the Car class.

11. Exercises

  1. 1. Create a Book class with auto-implemented properties for Title, Author, and Pages.
  1. 2. In Main(), create two Book objects, assign values to them, and print their details.

12. MCQs with Answers

Question 1

Which keyword is used to create a new object from a class?

Question 2

Variables declared inside a class are generally referred to as:

Question 3

Functions declared inside a class are referred to as:

Question 4

What is the default access modifier for a class member if none is specified?

Question 5

Which access modifier allows a variable to be accessed from anywhere?

Question 6

What feature in C# combines the syntax of a variable with the security of get/set methods?

Question 7

In public int Age { get; set; }, what does set do?

Question 8

What error occurs if you try to use an object without initializing it with new?

Q9. Is it considered a best practice to make all class fields public? a) Yes b) No, you should use private fields exposed through public Properties Answer: b) No
Question 10

What does the value keyword represent inside a Property setter?

13. Interview Questions

  • Q: Differentiate between a Field and a Property in C#. Why are properties preferred?
  • Q: Explain what the NullReferenceException is and what causes it when dealing with Objects.

14. Summary

Classes define the structure and behavior of your data, while Objects are the instances you interact with. By using Access Modifiers (public/private) and Properties (get/set), C# allows you to strictly control how data is accessed and modified, enforcing Encapsulation.

15. Next Chapter Recommendation

Right now, we create an object and *then* assign its properties line by line. In Chapter 14: Constructors and Destructors, we will learn how to initialize an object perfectly the exact moment it is created.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·