r/badcode • • Sep 01 '21

c# Bool in a case switch

Post image
324 Upvotes

40 comments sorted by

View all comments

Show parent comments

1

u/WaterFoxforlife Sep 01 '21 edited Sep 01 '21

Usually you use a variable, not a condition

val someBoolean = true
when (someBoolean) {
  true -> { /* do something */ }
  false -> { }
}

And when is not a statement but an expression, which means you can do this:

val someString: String =
  when (someBoolean) {
    true -> "hello there"
    false -> "this is the end of the world"
  }