0
0
C Sharp (C#)programming~30 mins

Record structs in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with Record Structs in C#
📖 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#)
Need a 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#)
Need a 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#)
Need a 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#)
Need a hint?

Use a foreach loop to print each book.Title from filteredBooks with Console.WriteLine(book.Title);.