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

List patterns in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
List Patterns in C#
📖 Scenario: You are working with a list of daily temperatures recorded over a week. You want to check if the list matches certain patterns to quickly understand the weather trend.
🎯 Goal: Build a C# program that uses list patterns to check if the temperature list starts with a cold day, ends with a hot day, or has a specific pattern in the middle.
📋 What You'll Learn
Create a list of integers called temperatures with exact values
Create a variable called patternToCheck to hold a pattern description
Use list patterns in a switch expression to match temperatures
Print the result of the pattern matching
💡 Why This Matters
🌍 Real World
Checking patterns in lists is useful in many real-world tasks like analyzing sensor data, processing user inputs, or validating sequences.
💼 Career
Understanding list patterns helps in writing clean, readable code for data processing, which is valuable in software development and data analysis jobs.
Progress0 / 4 steps
1
Create the temperature list
Create a list of integers called temperatures with these exact values: 12, 15, 20, 25, 30, 35, 40.
C Sharp (C#)
Need a hint?

Use List<int> and initialize it with the exact numbers inside curly braces.

2
Add a pattern description variable
Create a string variable called patternToCheck and set it to an empty string "".
C Sharp (C#)
Need a hint?

Declare a string variable and assign it an empty string.

3
Use list patterns to check the temperatures
Use a switch expression on temperatures to set patternToCheck to:
- "Starts cold" if the first temperature is less than 15,
- "Ends hot" if the last temperature is greater than 35,
- "Warm middle" if the middle three temperatures are between 20 and 30 inclusive,
- Otherwise, set it to "No pattern".
Use list patterns with temperatures in the switch expression.
C Sharp (C#)
Need a hint?

Use list patterns with switch and when guards to check the first and last elements, and a pattern for the middle three.

4
Print the pattern result
Write a Console.WriteLine statement to print the value of patternToCheck.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(patternToCheck); to show the result.