C# Programming Labs

Master modern C# features: records, structs, pattern matching with switch expressions, and nullable reference types.

Records, Pattern Matching & Nullables - Module 7

Leverage C# 9+ features for cleaner, safer code.

Lab 19: Records & Structs
Expert
Coding Challenge
Your Task: Define a record and a readonly struct, then demonstrate value equality and the with expression.

Detailed Requirements:
1. Create a positional record: public record Person(string Name, int Age); 2. Create a readonly struct: public readonly struct Point { public double X { get; init; } public double Y { get; init; } public Point(double x, double y) { X = x; Y = y; } public override string ToString() => $"({X}, {Y})"; } 3. Demonstrate value equality: Create two Person instances with same data and compare them with ==.
4. Use the with expression: Create a modified copy of a record.
5. Use init properties: Instantiate struct with object initializer.

💡 Pro Tips:
• Records provide value-based equality, immutability, and deconstruction by default
• Use readonly struct for small, immutable value types to avoid defensive copies
• with expression creates a shallow copy with specified changes
• Positional records auto-generate Deconstruct method

Expected Output: Person 1: Person { Name = Alice, Age = 30 } Person 2: Person { Name = Alice, Age = 30 } Are equal: True Person 3 (with): Person { Name = Alice, Age = 31 } Point: (3, 4)

Requirements Checklist

Define a positional record Person
Define a readonly struct Point
Create two Person instances and compare equality
Use the with expression to create modified copy
Use init properties on struct
Print all results to console
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Record syntax: public record Person(string Name, int Age);
• Readonly struct: public readonly struct Point { ... }
• With expression: var p2 = p1 with { Age = 31 };
• Init property: public double X { get; init; }
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 20: Pattern Matching & Switch Expressions
Expert
Coding Challenge
Your Task: Use C# pattern matching with switch expressions, type patterns, property patterns, and relational patterns.

Detailed Requirements:
1. Create a hierarchy: abstract record Shape; record Circle(double Radius) : Shape; record Rectangle(double Width, double Height) : Shape; 2. Switch expression with type patterns: Calculate area based on shape type.
3. Property patterns: Match on property values.
4. Relational patterns: Use <, >, and, or in patterns.
5. Discard pattern: Use _ for default case.

💡 Pro Tips:
• Switch expressions are more concise than switch statements
• Combine patterns with and, or, not
• Property patterns: Circle { Radius: > 5 }
• Always include a discard _ or exhaustive patterns

Expected Output: Circle area: 78.54 Rectangle area: 24 Size of 78.54: Large Size of 24: Medium Describe 10: Positive even Describe -3: Negative

Requirements Checklist

Define Shape hierarchy with records
Use switch expression with type patterns
Use property patterns for matching
Use relational patterns (<, >, and, or)
Include discard pattern _ for default
Print pattern match results
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Switch expression: shape switch { Circle c => ..., _ => ... }
• Relational: > 50, > 10 and <= 50
• Property pattern: Circle { Radius: > 5 }
• Guard clause: > 0 when n % 2 == 0
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 21: Nullable Reference Types
Expert
Coding Challenge
Your Task: Enable nullable reference types and safely handle nullable values using null-conditional, null-coalescing, and pattern matching.

Detailed Requirements:
1. Enable nullable context: Add #nullable enable directive.
2. Declare nullable and non-nullable types: string nonNull = "hello"; // Cannot be null string? maybeNull = null; // Can be null 3. Use null-conditional operator: maybeNull?.Length
4. Use null-coalescing operator: maybeNull ?? "default"
5. Use null-coalescing assignment: maybeNull ??= "assigned"
6. Pattern match on null: is not null and is null patterns.

💡 Pro Tips:
• Nullable annotations help catch NullReferenceException at compile time
• Use ! (null-forgiving) only when you're certain value is non-null
• Prefer is not null over != null for pattern matching
• ??= assigns only if current value is null

Expected Output: nonNull length: 5 maybeNull length: (null) With default: default value After ??= : now assigned name is not null: Alice FindPerson(1): Found Alice FindPerson(99): Not found

Requirements Checklist

Enable nullable context with #nullable enable
Declare nullable (string?) and non-nullable types
Use null-conditional operator (?.)
Use null-coalescing (??) and ??= operators
Use is null / is not null patterns
Print all null-handling results
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Enable: #nullable enable at top of file
• Nullable type: string? maybeNull = null;
• Null-conditional: obj?.Property
• Coalescing: value ?? "default", value ??= "assign"
• Pattern: if (x is not null)
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below