Last active
September 6, 2022 06:12
-
-
Save EifelMono/ba65235952e0a2fb6eb9f2119464b98b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Console.WriteLine("Anonymous pattern matching"); | |
var text = "Hello"; | |
var person = new PersonRecord("Andreas", "Klapperich", 11); | |
var output = new { Text = text, Person = person } switch | |
{ | |
{ Text: { }, Person: null } content => $"{content.Text} Unknown", | |
{ Text: { }, Person: { Name: "Andreas", LastName: "Klapperich", Age:> 0 } } content => $"{content.Text} ANDREAS KLAPPERICH", | |
{ Text: { }, Person: { Name: { }, LastName: { } } } content => $"{content.Text} {content.Person.Name} {content.Person.LastName}", | |
{ Text: { }, Person: { Name: null, LastName: { } } } content => $"{content.Text} ? {content.Person.LastName}", | |
{ Text: { }, Person: { Name: { }, LastName: null } } content => $"{content.Text} {content.Person.Name} ?", | |
{ Text: { }, Person: { } } content => $"{content.Text} everybody", | |
_ => "Something went wrong" | |
}; | |
Console.WriteLine(output); | |
if (new { Text = text, Person = person } is { Text: "Hello", Person: { Name: "Andreas" } } ifContent) | |
{ | |
Console.WriteLine(ifContent.Text); | |
Console.WriteLine(ifContent.Person.Name); | |
Console.WriteLine(ifContent.Person.LastName); | |
} | |
public record PersonRecord(string Name, string LastName, int Age); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anonymous Pattern matching