Package with two properties: Type (string) and Weight (int).package with specific values.maxWeight to set the weight limit.switch expression with positional patterns to decide the handling message.Package with two properties: Type (string) and Weight (int).package with specific values.maxWeight to set the weight limit.switch expression with positional patterns to decide the handling message.Package with two properties: string Type and int Weight. Then create a variable called package and set it to new Package("Fragile", 15).Use the record keyword to create a simple data container with two properties.
maxWeight and set it to 20.This variable will be used to compare the package weight later.
handling and set it using a switch expression on package. Use positional patterns to match Type and Weight as follows: if Type is "Fragile" and Weight is less than or equal to maxWeight, set handling to "Handle with care"; if Type is "Heavy" and Weight is greater than maxWeight, set handling to "Use forklift"; otherwise, set handling to "Standard handling".Use the switch expression with positional patterns and when clauses for conditions.
Console.WriteLine statement to print the handling variable.Use Console.WriteLine(handling); to show the result.
What is the main purpose of positional patterns in C#?
record Point(int X, int Y);
Using case Point(var x, var y): inside a switch statement helps to:
case Point(var x, var y):.Which of the following is the correct syntax for using a positional pattern with a record Person(string Name, int Age) inside a switch expression?
switch(person) {
case ???:
// code
break;
}Type(var1, var2).Person(var name, var age). Others use invalid syntax for positional patterns.What is the output of the following code?
record Point(int X, int Y);
var p = new Point(3, 4);
switch (p)
{
case Point(0, 0):
Console.WriteLine("Origin");
break;
case Point(var x, var y) when x == y:
Console.WriteLine("On diagonal");
break;
case Point(var x, var y):
Console.WriteLine($"Point at ({x}, {y})");
break;
}Identify the error in this code snippet using positional patterns:
record Rectangle(int Width, int Height);
var rect = new Rectangle(5, 10);
switch(rect)
{
case Rectangle(var w, var h):
Console.WriteLine($"Width: {w}, Height: {h}");
break;
case Rectangle(w, h):
Console.WriteLine("Invalid pattern");
break;
}var or a type.Rectangle(w, h) without var, which is invalid syntax.Given the record Person(string Name, int Age), write a switch expression using positional patterns that returns:
Which of the following switch expressions correctly implements this?
age < 13 for Child, age >= 13 && age <= 19 for Teen, and default Adult case. person switch {
Person(_, var age) when age < 13 => "Child",
Person(_, var age) when age > 13 && age <= 19 => "Teen",
Person(_, _) => "Adult"
}; misses age=13 (falls to Adult) because Teen condition uses > 13. person switch {
Person(var name, var age) when age < 13 => "Child",
Person(var name, var age) when age > 13 && age < 19 => "Teen",
_ => "Adult"
}; excludes age == 13 and 19 for Teen. person switch {
Person(var name, var age) when age <= 13 => "Child",
Person(var name, var age) when age >= 13 && age <= 19 => "Teen",
Person(var name, var age) => "Adult"
}; overlaps Child and Teen at age 13.Pass the test and this pattern moves unknown