Author Topic: Playing with: Simple Pattern matching in if statement  (Read 649 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Playing with: Simple Pattern matching in if statement
« on: January 03, 2024, 06:11:46 PM »
Code - C#: [Select]
  1. global using static System.Console;  // just to save noise writing Console. prefix
  2.  
  3. // Add and remove the "" to change object o between string and int.
  4. object o = "3";
  5. int j = 4;
  6. if (o is int i)     // <= here be magic !
  7. {
  8.   WriteLine($"{i} x {j} = {i * j}");
  9. }
  10. else {WriteLine("o is not an int so it cannot multiply!"); }
  11.  
  12.  
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MickD

  • King Gator
  • Posts: 3639
  • (x-in)->[process]->(y-out) ... simples!
Re: Playing with: Simple Pattern matching in if statement
« Reply #1 on: January 03, 2024, 07:33:37 PM »
nice!  :-)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: Playing with: Simple Pattern matching in if statement
« Reply #2 on: January 04, 2024, 01:04:02 AM »
Yes Mick,

There has been a lot of nice stuff in the last 5 or 6 years that has been ignored because we were stuck with Framework 4.8

I just need to wrap my old brain around it all  :-D
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

gile

  • Gator
  • Posts: 2518
  • Marseille, France
Re: Playing with: Simple Pattern matching in if statement
« Reply #3 on: January 04, 2024, 02:16:08 AM »
Hi Kerry,

This was already available with C# 7 (and .NET Framework), see this topic.
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2518
  • Marseille, France
Re: Playing with: Simple Pattern matching in if statement
« Reply #4 on: January 04, 2024, 03:42:37 AM »
The same can been written with a switch statement (C# 7):
Code - C#: [Select]
  1. switch (o)
  2. {
  3.     case int i:
  4.         WriteLine($"{i} x {j} = {i * j}"); break;
  5.     default:
  6.         WriteLine("o is not an int so it cannot multiply!"); break;
  7. }

C# 8 introduced "switch expressions":
Code - C#: [Select]
  1. WriteLine(o switch
  2. {
  3.     int i => $"{i} x {j} = {i * j}",
  4.     _ => "o is not an int so it cannot multiply!"
  5. });

Speaking English as a French Frog

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: Playing with: Simple Pattern matching in if statement
« Reply #5 on: January 04, 2024, 04:21:37 AM »
Thanks Gilles,
I missed that.



Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

gile

  • Gator
  • Posts: 2518
  • Marseille, France
Re: Playing with: Simple Pattern matching in if statement
« Reply #6 on: January 04, 2024, 05:31:24 PM »
This way also works:
Code - C#: [Select]
  1. WriteLine(
  2.     o is int i ?
  3.     $"{i} x {j} = {i * j}" :
  4.     "o is not an int so it cannot multiply!");
Speaking English as a French Frog