Showing posts with label C# 11 String interpolation Raw string literals Relational patterns. Show all posts
Showing posts with label C# 11 String interpolation Raw string literals Relational patterns. Show all posts

Saturday, 4 March 2023

Trøndersk dialekttemp with C# 11 og and patterns

This article demonstrates the use of relational patterns in C# 11. First off, relational patterns allow us to test how a given value /variable compares to constants. If we want to have multiple conditions we can use and operator not shown here. I have here different conditions / intervals for temperaturs outputting the temperature and description of the weather in some local language of mine from Norway (Trøndersk / Trondheim city).
	   
    
foreach (var iteration in Enumerable.Range(0, 20)){
    var tæmpen = new Random().Next(-60, 60);
    
    var været = $"Været e i dag {tæmpen}C og på Trondheimsdialækt: {tæmpen switch 
    {
        <= -50 => "Du træng eitt nytt termometer. For kaldt!",
        <= -35 => "Småfuggel'n dætt dau fra trær'n",
        <= -30 => "Båinnspeika",
        <= -25 => "Få inn katta!",
        <= -20 => "Gnallerfrost",
        <= -10 => "Kjøle kaaalt",
        <= -5  => "Kaillvoli",
        <= 5 => "Kaillhustri",
        <= 10 => "Julivær",
        <= 15 => "Godværsle",
        <= 20 => "Kjøle varmt",
        <= 25 => "Råådeili",
        <= 30 => "Steikvarmt",
        <= 40 => "Søkke heitt",
        <= 50 => "Kokheitt",
        _ => "Du træng eitt nytt termometer. For varmt!"
    }}";
    
    System.Console.WriteLine(været);
} 
    
    
As we can see, in C# 11, we can do a lot more inside string interpolation expressions and now allow multiple lines, including relational patterns. It can be quite handy to use, when .NET 7 and C# 11 reaches mainstream usage. The text here is based upon a dialect based thermometer, available for purchase from here: https://dialekttempen.no/butikk/termometer/fylker/sor-trondelag/trondheim-munkholmen/#&gid=1&pid=1