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

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

Choose your learning style9 modes available
Using Property Patterns in C#
📖 Scenario: You are building a simple program to check if a person is an adult living in a specific city. You will use property patterns to make your code clear and easy to read.
🎯 Goal: Create a Person class with properties, then use property patterns to check if a person is an adult living in "New York".
📋 What You'll Learn
Create a Person class with Name, Age, and City properties
Create an instance of Person with specific values
Use a property pattern in an if statement to check if the person is at least 18 years old and lives in "New York"
Print a message based on the property pattern check
💡 Why This Matters
🌍 Real World
Property patterns help write clear and readable code when checking multiple properties of objects, common in user data validation or filtering.
💼 Career
Understanding property patterns is useful for writing maintainable C# code in jobs involving backend development, data processing, or any object-oriented programming tasks.
Progress0 / 4 steps
1
Create the Person class
Create a public class called Person with three public properties: Name of type string, Age of type int, and City of type string.
C Sharp (C#)
Need a hint?

Use public class Person and add three properties with get and set.

2
Create a Person instance
Create a variable called person and assign it a new Person object with Name set to "Alice", Age set to 20, and City set to "New York".
C Sharp (C#)
Need a hint?

Use object initializer syntax to set the properties when creating person.

3
Use property pattern to check person details
Write an if statement that uses a property pattern to check if person has Age greater than or equal to 18 and City equal to "New York".
C Sharp (C#)
Need a hint?

Use the is keyword with a property pattern inside the if condition.

4
Print the result
Add an else block to print "Person does not meet the criteria." if the property pattern check fails. Then run the program to see the output.
C Sharp (C#)
Need a hint?

Use an else block with a Console.WriteLine to print the alternative message.