Casting with as and is operators
📖 Scenario: Imagine you have a list of different shapes, but you want to work only with circles. You need to check if each shape is a circle and then get its radius.
🎯 Goal: You will create a list of shapes, check each shape's type using is and as operators, and print the radius of circles.
📋 What You'll Learn
Create a list called
shapes with different shape objectsCreate a variable called
circleCount to count circlesUse a
foreach loop with is operator to count circlesUse a
foreach loop with as operator to print circle radii💡 Why This Matters
🌍 Real World
In real programs, you often have collections of different objects and need to work with only some types safely.
💼 Career
Understanding type checking and casting is important for writing safe and clear code in many C# applications.
Progress0 / 4 steps