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

Collection initialization syntax in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Collection Initialization Syntax in C#
📖 Scenario: You are building a simple inventory system for a small bookstore. You want to store a list of book titles available in the store.
🎯 Goal: Create a list of book titles using collection initialization syntax, then add a few more titles, and finally display all the titles.
📋 What You'll Learn
Create a List<string> called books with three specific book titles using collection initialization syntax.
Add two more book titles to the books list.
Use a foreach loop to print each book title.
💡 Why This Matters
🌍 Real World
Managing collections of items like books, products, or users is common in software applications.
💼 Career
Understanding collection initialization and manipulation is essential for software developers working with data storage and retrieval.
Progress0 / 4 steps
1
Create a list of books using collection initialization syntax
Create a List<string> called books and initialize it with these exact book titles: "The Hobbit", "1984", and "Pride and Prejudice" using collection initialization syntax.
C Sharp (C#)
Need a hint?

Use new List<string> { ... } to initialize the list with values.

2
Add more books to the list
Add two more book titles to the books list: "To Kill a Mockingbird" and "The Great Gatsby" using the Add method.
C Sharp (C#)
Need a hint?

Use books.Add("Book Title") to add new titles.

3
Print all book titles using a foreach loop
Use a foreach loop with the variable book to iterate over books and print each book title using Console.WriteLine(book).
C Sharp (C#)
Need a hint?

Use foreach (string book in books) to loop through the list and Console.WriteLine(book) to print each title.

4
Run the program to display all book titles
Run the program to display all the book titles in the console. The output should list all five books, each on its own line.
C Sharp (C#)
Need a hint?

Make sure your program prints each book title on its own line.