Source: Ch1_OOP.pdf
Classes and Objects
OOP concepts, members, constructors, destructors, and value/reference behavior.
Defining a Class and Creating Objects
A class is a blueprint for objects. An object is an instance created with new. The class defines the fields, properties, and methods every instance will have.
public class Student
{
public string Name = "";
public int Age { get; set; }
public void PrintInfo()
{
Console.WriteLine($"Name: {Name}, Age: {Age}");
}
}
Student s = new Student();
s.Name = "Alice";
s.Age = 22;
s.PrintInfo();Output
Name: Alice, Age: 22
Classes and Objects — Exercise 1
Access class members through the object reference using the dot (.) operator.
Fix the incorrect member access
Hint
Declare the object with new Car() before accessing its members.
What keyword creates a new instance of a class?
Fields
Fields are variables declared directly inside a class. Public fields are accessible from outside; private fields are not. All fields get a default value (0, false, null) if not initialized.
class Rectangle
{
public double Width;
public double Height;
private string color = "white";
public double Area() => Width * Height;
public string GetColor() => color;
}
Rectangle r = new Rectangle();
r.Width = 5;
r.Height = 3;
Console.WriteLine(r.Area());
Console.WriteLine(r.GetColor());Output
15 white
Fields — Exercise 1
Private fields can only be read or modified through the class's own methods.
Fix the direct access to a private field
Hint
balance is private and cannot be accessed directly from outside the class. Remove the direct assignment.
What access modifier makes a field visible only within its own class?
Properties
Properties are preferred over public fields because they let you add logic in get and set accessors. Auto-properties { get; set; } generate the backing field automatically.
class Person
{
private int age;
public int Age
{
get { return age; }
set
{
if (value >= 0) age = value;
}
}
public string Name { get; set; } = "";
}
Person p = new Person();
p.Name = "Bob";
p.Age = 30;
p.Age = -5; // ignored by setter
Console.WriteLine($"{p.Name} is {p.Age}");Output
Bob is 30
Properties — Exercise 1
A read-only property exposes a value without allowing external code to change it. Add a set accessor to make it writable.
Add write access to a read-only property
Hint
The property currently has only a get accessor. Add set { name = value; } to allow writing.
What keyword inside a set accessor refers to the incoming value?
Constructors
A constructor runs automatically when an object is created with new. It has the same name as the class and no return type. The default (parameterless) constructor is generated by the compiler if you do not define any.
class Circle
{
public double Radius;
public Circle()
{
Radius = 1.0;
}
public Circle(double r)
{
Radius = r;
}
public double Area() => Math.PI * Radius * Radius;
}
Circle c1 = new Circle();
Circle c2 = new Circle(5);
Console.WriteLine(c1.Radius);
Console.WriteLine(Math.Round(c2.Area(), 2));Output
1 78.54
Constructors — Exercise 1
A constructor must have the exact same name as its class. A different name is treated as a regular method.
Fix the misnamed constructor
Hint
Constructors have no return type — not even void. Remove the void keyword.
When is a constructor called?
Destructors
A destructor (~ClassName) is called by the garbage collector when an object is no longer referenced. You rarely need to write one explicitly in modern C#.
class Resource
{
public Resource() => Console.WriteLine("Constructed");
~Resource() => Console.WriteLine("Destructed");
}
Resource r = new Resource();
Console.WriteLine("Object in use");Output
Constructed Object in use Destructed
What is the correct syntax for a destructor named MyClass?
Value vs Reference Types
Value types (int, double, struct) copy their data on assignment. Reference types (class, array, string) copy the address — both variables then point to the same object.
int a = 10;
int b = a;
b = 99;
Console.WriteLine(a); // 10 — unchanged
int[] arr1 = { 1, 2, 3 };
int[] arr2 = arr1;
arr2[0] = 99;
Console.WriteLine(arr1[0]); // 99 — both point to same arrayOutput
10 99
Value vs Reference — Exercise 1
Assigning one array to another does not copy the elements — it copies the reference. Both variables then share the same data.
Make an independent copy of the array
Hint
Use Clone() or Array.Copy() to create an independent copy of an array.
What happens when you assign one class instance to another variable?