C
C#3y ago
SWEETPONY

✅ Is it possible to write better switch/case?

I have following:
_ = delivery.Topic switch
{
_ when delivery.Topic.Contains( "event" )
=> Brush.Color = Colors.Yellow,
_ when delivery.Topic.Contains( "command" )
=> Brush.Color = Colors.Green,
_ => Brush.Color = Colors.Beige
};
_ = delivery.Topic switch
{
_ when delivery.Topic.Contains( "event" )
=> Brush.Color = Colors.Yellow,
_ when delivery.Topic.Contains( "command" )
=> Brush.Color = Colors.Green,
_ => Brush.Color = Colors.Beige
};
6 Replies
SWEETPONY
SWEETPONYOP3y ago
I think _ looks a little bit ugly.. How I can improve my code?
jcotton42
jcotton423y ago
just use if
Jimmacle
Jimmacle3y ago
kind of an abuse of a switch expression if you ask me
jcotton42
jcotton423y ago
instead of... that
Jimmacle
Jimmacle3y ago
you're discarding all the parts that make that syntax useful
SWEETPONY
SWEETPONYOP3y ago
yeah, ur right I will rewrite my code

Did you find this page helpful?