📖 Scenario: You are building a simple program to store information about books in a library. Each book has a title and a number of pages.
🎯 Goal: Create a record struct called Book to hold the title and pages of a book. Then create a list of books, filter books with more than 300 pages, and print their titles.
📋 What You'll Learn
Create a record struct named Book with two properties: string Title and int Pages
Create a list of Book instances with exact titles and pages
Create a variable pageThreshold set to 300
Use a foreach loop to find books with pages greater than pageThreshold
Print the titles of the filtered books
💡 Why This Matters
🌍 Real World
Record structs are useful for storing small, immutable data like coordinates, settings, or simple records in applications such as libraries, games, or data processing.
💼 Career
Understanding record structs helps you write clean, efficient C# code for data storage and manipulation, a common task in software development jobs.
Progress0 / 4 steps
1
Create the Book record struct and list of books
Create a record struct named Book with two properties: string Title and int Pages. Then create a list called books with these exact entries: new Book("C# Basics", 250), new Book("Advanced C#", 400), new Book("LINQ in Action", 320).
C Sharp (C#)
Hint
Use the syntax public record struct Book(string Title, int Pages); to create the record struct. Then create a List<Book> with the given books.
2
Add a page threshold variable
Create an int variable called pageThreshold and set it to 300.
C Sharp (C#)
Hint
Simply write int pageThreshold = 300; below the list of books.
3
Filter books with pages greater than the threshold
Create a List<Book> called filteredBooks. Use a foreach loop with variables book to iterate over books. Inside the loop, add book to filteredBooks if book.Pages is greater than pageThreshold.
C Sharp (C#)
Hint
Create an empty list filteredBooks. Then use a foreach loop to check each book's pages and add it to filteredBooks if it has more pages than pageThreshold.
4
Print the titles of filtered books
Use a foreach loop with variable book to iterate over filteredBooks. Inside the loop, print the book.Title using Console.WriteLine.
C Sharp (C#)
Hint
Use a foreach loop to print each book.Title from filteredBooks with Console.WriteLine(book.Title);.
Practice
(1/5)
1. What is a key feature of record struct in C#?
easy
A. It provides value-based equality and immutability by default.
B. It allows inheritance from other structs.
C. It can only be used for reference types.
D. It does not support property declarations.
Solution
Step 1: Understand record struct purpose
Record structs combine struct's lightweight nature with record's value-based equality and immutability.
Step 2: Analyze options
It provides value-based equality and immutability by default. correctly states value-based equality and immutability. Options B, C, and D are incorrect because structs cannot inherit, record structs are value types, and they support properties.
Final Answer:
It provides value-based equality and immutability by default. -> Option A
Quick Check:
record struct = value equality + immutability [OK]
Hint: Remember: record structs behave like immutable value containers [OK]
Common Mistakes:
Thinking record structs support inheritance
Confusing record structs with classes
Assuming record structs are reference types
2. Which of the following is the correct syntax to declare a record struct named Point with two properties X and Y of type int?
easy
A. struct record Point { int X; int Y; }
B. record Point struct { int X; int Y; }
C. record struct Point(int X, int Y);
D. record struct Point { int X; int Y; }
Solution
Step 1: Recall record struct declaration syntax
The concise syntax for record structs uses positional parameters: record struct Name(type prop1, type prop2);
Step 2: Compare options
record struct Point(int X, int Y); matches the correct syntax. record struct Point { int X; int Y; } is close but missing parentheses for positional parameters. Options A and C have incorrect order or keywords.
Final Answer:
record struct Point(int X, int Y); -> Option C
Quick Check:
record struct with positional params = record struct Point(int X, int Y); [OK]
Hint: Use parentheses for positional properties in record structs [OK]
Common Mistakes:
Swapping 'record' and 'struct' keywords
Omitting parentheses for positional parameters
Using braces instead of parentheses for positional syntax
3. What will be the output of the following code?
record struct Point(int X, int Y); var p1 = new Point(3, 4); var p2 = new Point(3, 4); Console.WriteLine(p1 == p2);
medium
A. True
B. False
C. Compilation error
D. Runtime exception
Solution
Step 1: Understand equality in record structs
Record structs provide value-based equality by default, so two instances with same values are equal.
Step 2: Analyze the code output
Both p1 and p2 have X=3 and Y=4, so p1 == p2 evaluates to true.
Final Answer:
True -> Option A
Quick Check:
record struct equality = value equality [OK]
Hint: Record structs compare values, not references [OK]
Common Mistakes:
Assuming == compares references for structs
Expecting compilation error for == operator
Confusing record structs with classes for equality
4. Identify the error in this code snippet:
record struct Rectangle(int Width, int Height); var rect = new Rectangle(5, 10); rect.Width = 7; Console.WriteLine(rect);
medium
A. Rectangle must be a class to allow property assignment.
B. Missing semicolon after record struct declaration.
C. Console.WriteLine cannot print record structs.
D. Cannot assign to 'Width' because it is readonly.
Solution
Step 1: Understand immutability of record structs
Record structs with positional parameters generate readonly properties by default.
Step 2: Analyze the assignment line
Trying to assign rect.Width = 7; causes a compile-time error because Width is readonly.
Final Answer:
Cannot assign to 'Width' because it is readonly. -> Option D
Quick Check:
Positional record struct properties are readonly [OK]
Hint: Positional record struct properties are readonly by default [OK]
Common Mistakes:
Trying to modify readonly properties
Confusing record structs with mutable classes
Ignoring compiler error messages
5. You want to create a lightweight data type to store a 3D point with coordinates X, Y, and Z. You want it to be immutable, support value equality, and print nicely. Which is the best way to declare it using record structs?
hard
A. struct Point3D { public int X; public int Y; public int Z; }
B. record struct Point3D(int X, int Y, int Z);
C. class Point3D { public int X { get; set; } public int Y { get; set; } public int Z { get; set; } }
D. record class Point3D(int X, int Y, int Z);
Solution
Step 1: Identify requirements
Needs lightweight, immutable, value equality, and nice printing.
Step 2: Match requirements to options
record struct Point3D(int X, int Y, int Z); uses record struct with positional parameters, providing all requirements. struct Point3D { public int X; public int Y; public int Z; } is mutable struct without value equality or nice printing. class Point3D { public int X { get; set; } public int Y { get; set; } public int Z { get; set; } } is mutable class, heavier and reference type. record class Point3D(int X, int Y, int Z); is record class, reference type, not lightweight.
Final Answer:
record struct Point3D(int X, int Y, int Z); -> Option B
Quick Check:
record struct = lightweight + immutable + value equality [OK]
Hint: Use record struct with positional params for immutable lightweight data [OK]
Common Mistakes:
Choosing class instead of struct for lightweight data
Using mutable fields instead of readonly properties
Confusing record class with record struct
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.