Module 3 · Collections and objects: organise your data · Lesson 12 of 20
Group a task and its behaviour in a class
Two tasks each need a title and a completion flag. How do we keep the right values together? Create two objects and change just one.
A task has information that belongs together: its title and whether it is done. Keeping separate lists of titles and bool values makes it easy for the positions to get out of step. A class lets us name one type that groups this state and the operations that use it. Here the class is called StudyTask. Each object made from that class can hold different state.
Predict two separate tasks
Before running, predict the title and bool printed on each line. Each new StudyTask(...) creates a new object. The class declaration follows the top-level program statements.
using System;
StudyTask first = new StudyTask("Read variables");
StudyTask second = new StudyTask("Practise loops");
first.MarkDone();
Console.WriteLine($"{first.Title}: {first.IsDone}");
Console.WriteLine($"{second.Title}: {second.IsDone}");
class StudyTask
{
public string Title { get; set; }
public bool IsDone { get; set; }
public StudyTask(string title)
{
Title = title;
IsDone = false;
}
public void MarkDone()
{
IsDone = true;
}
}What is printed?
Show answer
Correct answer: B
first.MarkDone changes first. second was created separately and remains unfinished.
Option A: MarkDone is called only on first.
Option C: The method is called on first, not second.
Option D: Two new expressions create two objects.
Hint: Read the name before the dot in first.MarkDone().
Read the class in small pieces
The declaration public string Title { get; set; } defines a property. get allows its value to be read; set allows it to be assigned. IsDone is a bool property. public makes these members available to the code using the object. We use straightforward writable properties here; more restrictive designs can come later.
The constructor has the same name as the class and no return type. It runs when this form of new creates an object. Its title parameter provides the initial Title, and we explicitly start IsDone at false. The parameter title and property Title have different names because C# treats letter case as significant.
new StudyTask("Read variables")
|
v
object first
Title = "Read variables"
IsDone = false
| first.MarkDone()
v
IsDone = true
new StudyTask("Practise loops") creates another object.Which statement about the constructor is correct?
Show answer
Correct answer: C
Each constructor call initializes the newly created StudyTask object.
Option A: A constructor has no declared return type.
Option B: The two new expressions each call the constructor.
Option D: An ordinary constructor uses the class name.
Hint: Compare the two new lines in the program.
MarkDone is an instance method: it operates on the object before the dot. It returns no value, so its return type is void. Inside it, IsDone = true changes that object. This method is intentionally simple enough to trace without learning inheritance or interfaces.
Your turn: create a validated task
Ask for a title. Reject null, empty text and spaces. Otherwise trim the title, create one task, mark it done and print its state. Try your version before reading the complete solution.
using System;
Console.WriteLine("Task title:");
string? title = Console.ReadLine();
if (string.IsNullOrWhiteSpace(title))
{
Console.WriteLine("Title cannot be blank.");
}
else
{
StudyTask task = new StudyTask(title.Trim());
task.MarkDone();
Console.WriteLine($"{task.Title}: {task.IsDone}");
}
class StudyTask
{
public string Title { get; set; }
public bool IsDone { get; set; }
public StudyTask(string title)
{
Title = title;
IsDone = false;
}
public void MarkDone()
{
IsDone = true;
}
}With two spaces, Practise methods, and two more spaces as input, expect Practise methods: True after the prompt. With an empty line, expect Title cannot be blank. The constructor in this example stores what it receives; our input boundary performs the blank-title validation. A constructor does not automatically enforce rules you have not written.
Why call title.Trim() only inside the else branch?
Show answer
Correct answer: B
The earlier validation makes it safe and meaningful to trim this accepted title.
Option A: Trim is a string method and can be used in other suitable places.
Option C: It returns trimmed text; it does not update unrelated objects.
Option D: Trim returns a string.
Explain it back
Point to the class, each object, the constructor, one property and one instance method. Then create a third task and predict its initial IsDone value. Use “the class describes the type; new creates an object” in your explanation.
References: Microsoft - constructors and properties.
Quick reference
- A class defines a custom type.
- new creates an object and calls a constructor.
- Properties expose values through get and set.
- A constructor initializes a new object.
- An instance method acts on the object before the dot.
- Write validation rules explicitly.
TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·