✅ Switch - return 🤔 ( attached )
I’m getting an explanation on switch in C# and I don’t quite get what the last return outside the scope has to do with it - does. Any idea?

24 Replies
pretend that the value of
colorNl
is "rood"
and follow the path the code takes
return
isn't part of the switch itself, but i don't know what the context is outside of this screenshotThe code shown here seems a bit out of context. Looks like it should be inside of a method
Then, the return would make more sense
if colorNl is "groen" code flows like this:

if it doesnt match any of the cases it flows like this:

if that makes sense
break exits the switch
couldn't you just return instead of the ?
they could but i think the point is to learn the switch case semantics
yeah, the code isn't great but it conveys how the syntax is used
realistically you could just use a switch expression here and make it a lot more concise
switch expression, interesting
Haven't seen that very often
it's fairly new so older learning material won't have it
specifically courses based on .NET Framework
is it something you would use tho?
how is it performance wise compared to a normal switch statement?
if there's any difference it's not enough to matter 99.99% of the time
all the time
really?
it doen't seem THAT usefull tbh
and yes, any time i'm using a switch just to get a value and not do something more complex i use the expression style
it's like 1/3 the loc
hmm okay


how does this one work?
what is the value on the left?
Pattern matching overview - C#
Learn about pattern matching expressions in C#
oh
so multiple inputs?!
Yes. You can test the values within the 'switched' object, so he’s basically checking if
RequestUrl.Host
matches the given string AND AbsoluthPath
matches the other string. If both pass, only then the HandleCloudfare…
method is being called, otherwise it will test the next case.Okii
Thanks for the responses people 👍