Ejercicios de C#

Pattern Matching Classifier

Modern C# patterns keep type extraction and validation in one expression.

How it works

Classify mixed values with property and relational patterns.

The string property pattern checks Length after confirming the runtime type.

Runnable C# example

C#
Run code →
Program.cs
using System;
class Program
{
    static string Classify(object? value) => value switch
    {
        int and >= 18 => "adult age", string { Length: 0 } => "empty text",
        string text => $"text({text.Length})", null => "null", _ => "other"
    };
    static void Main() => Console.WriteLine(Classify("CSharp"));
}
Expected output
text(6)

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.