Complete the code to check if the object is an integer using pattern matching.
if (obj is [1] number) { Console.WriteLine($"Number is {number}"); }
The is keyword with int pattern checks if obj is an integer and assigns it to number.
Complete the code to use pattern matching in a switch expression to return a message based on the shape type.
string message = shape switch {
Circle c => $"Circle with radius {c.Radius}",
Rectangle r => $"Rectangle {r.Width}x{r.Height}",
[1] => "Unknown shape"
};The underscore _ is the discard pattern that matches anything not matched before, acting as a default case.
Fix the error in the pattern matching expression to correctly check if the object is a string with length greater than 5.
if (obj is string s && s.Length [1] 5) { Console.WriteLine("Long string"); }
The condition should check if the string length is greater than 5 using the '>' operator.
Fill both blanks to create a pattern matching expression that checks if the object is a Point with X greater than 0 and Y less than 0.
if (obj is Point [1] && [1].X > 0 && [1].Y [2] 0) { Console.WriteLine("Point in quadrant 4"); }
The variable p captures the Point object, and the condition checks if p.X is greater than 0 and p.Y is less than 0.
Fill all three blanks to create a switch expression that returns the area of shapes, using pattern matching with property patterns.
double area = shape switch {
Circle [1] => Math.PI * [2].Radius * [3].Radius,
Rectangle r => r.Width * r.Height,
_ => 0
};The variable c captures the Circle object, which is then used to access its Radius property twice to calculate the area.