Welcome,
Guest
. Please
login
or
register
.
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News:
Home
Help
Login
Register
TheSwamp
»
Code Red
»
.NET
»
ACAD with .NET8+
»
Topic:
Playing with: Simple Pattern matching in if statement
« previous
next »
Print
Pages: [
1
] |
Go Down
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]
global
using
static
System
.
Console
;
// just to save noise writing Console. prefix
// Add and remove the "" to change object o between string and int.
object
o
=
"3"
;
int
j
=
4
;
if
(
o
is
int
i
)
// <= here be magic !
{
WriteLine
(
$
"{i} x {j} = {i * j}"
)
;
}
else
{
WriteLine
(
"o is not an int so it cannot multiply!"
)
;
}
Logged
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!
WWW
Re: Playing with: Simple Pattern matching in if statement
«
Reply #1 on:
January 03, 2024, 07:33:37 PM »
nice!
Logged
"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
Logged
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
WWW
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
.
Logged
Speaking English as a French Frog
gile
Gator
Posts: 2518
Marseille, France
WWW
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]
switch
(
o
)
{
case
int
i
:
WriteLine
(
$
"{i} x {j} = {i * j}"
)
;
break
;
default
:
WriteLine
(
"o is not an int so it cannot multiply!"
)
;
break
;
}
C# 8 introduced "switch expressions":
Code - C#:
[Select]
WriteLine
(
o
switch
{
int
i
=>
$
"{i} x {j} = {i * j}"
,
_
=>
"o is not an int so it cannot multiply!"
}
)
;
Logged
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.
Logged
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
WWW
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]
WriteLine
(
o
is
int
i
?
$
"{i} x {j} = {i * j}"
:
"o is not an int so it cannot multiply!"
)
;
Logged
Speaking English as a French Frog
Print
Pages: [
1
] |
Go Up
« previous
next »
TheSwamp
»
Code Red
»
.NET
»
ACAD with .NET8+
»
Topic:
Playing with: Simple Pattern matching in if statement