Created
October 11, 2021 22:08
-
-
Save richlander/5bd7076419c898f2a4733b609b70d85f 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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
</Project> |
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
List<Status> statuses = new() | |
{ | |
new(Category.Normal, new(20, false, 20)), | |
new(Category.Warning, new(20, false, 60)), | |
new(Category.Danger, new(20, true, 60)), | |
new(Category.Danger, new(100, false, 20)) | |
}; | |
foreach (Status status in statuses) | |
{ | |
string message = status switch | |
{ | |
{Category: Category.Normal} => "Let the good times roll", | |
{Category: Category.Warning, Reading.PM25: >50 and <100} => "Check the air filters", | |
{Reading.PM25: >200 } => "There must be a fire somewhere. Don't go outside.", | |
{Reading.SmokeDetected: true } => "We have a fire!", | |
{Category: Category.Danger} => "Something is badly wrong", | |
_ => "Unknown status" | |
}; | |
Console.WriteLine(message); | |
} | |
record struct Reading(int Temperature, bool SmokeDetected, int PM25); | |
record struct Status(Category Category, Reading Reading); | |
enum Category | |
{ | |
Normal, | |
Warning, | |
Danger | |
} |
Is this and or is it or and how can you distinguish the two?
{Category: Category.Warning, Reading.PM25: >50 and <100}
@johanskoldekrans It is 'and'. For 'or' you could use {Category: Category.Warning} or {Reading.PM25: >50 and <100}
Excellent! Thank you @algonzalez
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Produces the following output:
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10#extended-property-patterns