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

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

Choose your learning style9 modes available
Relational patterns
📖 Scenario: You are building a simple program to check the age of a person and categorize them as a child, teenager, adult, or senior.
🎯 Goal: Create a program that uses relational patterns in a switch expression to print the correct age category based on the person's age.
📋 What You'll Learn
Create an integer variable age with the exact value 25.
Create a string variable category to hold the age group.
Use a switch expression with relational patterns to assign the correct category:
< 13 is "Child"
< 20 is "Teenager"
< 65 is "Adult"
Anything else is "Senior"
Print the category variable.
💡 Why This Matters
🌍 Real World
Age categories are used in many apps like games, websites, and services to customize content or permissions.
💼 Career
Understanding relational patterns helps you write clear and concise code for decision making in professional C# development.
Progress0 / 4 steps
1
Create the age variable
Create an integer variable called age and set it to 25.
C Sharp (C#)
Need a hint?

Use int age = 25; to create the variable.

2
Create the category variable
Create a string variable called category to hold the age group. Initialize it with an empty string "".
C Sharp (C#)
Need a hint?

Use string category = ""; to create the variable.

3
Use switch expression with relational patterns
Use a switch expression on the variable age with relational patterns to assign the correct string to category. Use these patterns:
< 13 returns "Child"
< 20 returns "Teenager"
< 65 returns "Adult"
Anything else returns "Senior".
C Sharp (C#)
Need a hint?

Use category = age switch { < 13 => "Child", < 20 => "Teenager", < 65 => "Adult", _ => "Senior" };

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

Use Console.WriteLine(category); to print the result.