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
newkeyword 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 theclass keyword. Inside the class, we define Fields (variables belonging to the class) and Methods (functions belonging to the class).
csharp
4. Creating an Object
Once the class is defined, you can create multiple Objects (Instances) in yourMain method using the new keyword.
csharp
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
*This is the core of Encapsulation!*
6. Fields vs. Properties
In modern C#, making fieldspublic 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
7. Mini Project: Employee Management System
csharp
8. OOP and Memory Explanation
When you writeCar 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 aNullReferenceException. 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 theCarclass.
11. Exercises
-
1.
Create a
Bookclass with auto-implemented properties forTitle,Author, andPages.
-
2.
In
Main(), create twoBookobjects, 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?
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
NullReferenceExceptionis 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.