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

First, Single, and their OrDefault variants in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Using First, Single, and Their OrDefault Variants in C#
📖 Scenario: You are working on a simple program that manages a list of book titles in a library. You want to find specific books using different methods that help you get the first match, a single match, or a safe default if no match is found.
🎯 Goal: Build a C# program that uses First, Single, FirstOrDefault, and SingleOrDefault methods to find books in a list safely and correctly.
📋 What You'll Learn
Create a list of book titles with exact values.
Add a search keyword variable.
Use First and Single methods to find books.
Use FirstOrDefault and SingleOrDefault methods to handle cases with no matches.
Print the results clearly.
💡 Why This Matters
🌍 Real World
Finding specific items in lists or collections is common in apps like libraries, stores, or contact lists.
💼 Career
Understanding these LINQ methods helps you write safer and clearer code when searching data in professional C# development.
Progress0 / 4 steps
1
Create a list of book titles
Create a List<string> called books with these exact titles: "C# Basics", "LINQ Fundamentals", "C# Basics", "Advanced C#".
C Sharp (C#)
Need a hint?

Use new List<string> { ... } to create the list with the exact titles.

2
Add a search keyword variable
Add a string variable called searchKeyword and set it to "C# Basics".
C Sharp (C#)
Need a hint?

Declare a string variable with the exact name and value.

3
Use First and Single to find books
Use First to find the first book in books that equals searchKeyword and store it in firstBook. Then use Single to find the single book in books that equals "LINQ Fundamentals" and store it in singleBook.
C Sharp (C#)
Need a hint?

Use books.First(book => book == searchKeyword) and books.Single(book => book == "LINQ Fundamentals").

4
Use OrDefault variants and print results
Use FirstOrDefault to find a book matching "Nonexistent Book" and store it in firstOrDefaultBook. Use SingleOrDefault to find a book matching "Advanced C#" and store it in singleOrDefaultBook. Then print all four variables: firstBook, singleBook, firstOrDefaultBook, and singleOrDefaultBook.
C Sharp (C#)
Need a hint?

Use FirstOrDefault and SingleOrDefault with the exact search strings. Use Console.WriteLine to print each result. Use ?? "null" to show "null" if the result is empty.