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

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

Choose your learning style9 modes available
Type Checking Patterns in C#
πŸ“– Scenario: Imagine you are building a simple program that processes different shapes. Each shape can be a Circle or a Rectangle. You want to check the type of each shape and calculate its area accordingly.
🎯 Goal: You will create a list of shapes, add a configuration variable for a minimum area threshold, use type checking patterns to calculate areas only for shapes that meet the threshold, and finally print the areas.
πŸ“‹ What You'll Learn
Create a list called shapes containing exactly two objects: a Circle with radius 3 and a Rectangle with width 4 and height 5.
Create a variable called minArea and set it to 10.
Use a foreach loop with type checking patterns to calculate the area of each shape only if it is a Circle or Rectangle and the area is greater than or equal to minArea. Store the results in a dictionary called areas with the shape type name as the key and the area as the value.
Print the areas dictionary.
πŸ’‘ Why This Matters
🌍 Real World
Type checking patterns help you write clear and safe code when working with different object types, such as shapes, UI elements, or messages.
πŸ’Ό Career
Understanding type checking patterns is important for C# developers to handle polymorphism, improve code readability, and avoid errors when processing mixed data.
Progress0 / 4 steps
1
Create the shapes list
Create a list called shapes containing exactly two objects: a Circle with Radius 3 and a Rectangle with Width 4 and Height 5. Use the provided Circle and Rectangle classes.
C Sharp (C#)
Need a hint?

Use List<object> to hold different shape objects. Add new Circle(3) and new Rectangle(4, 5) inside the list.

2
Add minimum area threshold
Create a variable called minArea and set it to 10.
C Sharp (C#)
Need a hint?

Just create a double variable named minArea and assign it the value 10.

3
Calculate areas using type checking patterns
Use a foreach loop with variables shape to iterate over shapes. Inside the loop, use type checking patterns with is to check if shape is a Circle or Rectangle. Calculate the area for each shape: for Circle, area = Ο€ x radiusΒ²; for Rectangle, area = width x height. Only add the shape type name and area to a dictionary called areas if the area is greater than or equal to minArea. Use Math.PI for Ο€.
C Sharp (C#)
Need a hint?

Use foreach (object shape in shapes). Inside, use if (shape is Circle c) and else if (shape is Rectangle r). Calculate area accordingly and add to areas dictionary only if area ≥ minArea.

4
Print the areas dictionary
Use a foreach loop with variables key and value to iterate over areas. Print each shape type and its area in the format: "{key}: {value:F2}" where {value:F2} formats the area to 2 decimal places.
C Sharp (C#)
Need a hint?

Use foreach (var (key, value) in areas) and Console.WriteLine($"{key}: {value:F2}") to print each shape and its area with two decimals.