How to do this in C#?

In Xojo you can do something like

   select case true

      case functionCallHereThatReturnsABoolean(with parameters)
               // code for functionCallHereThatReturnsABoolean
      case anotherFunctionCallHereThatReturnsABoolean(with parameters)
               // code for anotherFunctionCallHereThatReturnsABoolean
      else
              // code for else
  end select

I was trying to do something similar in C# with SWITCH but it doesnt seem to support anything like this
I resorted to a long if then type statement

``
if functionCallHereThatReturnsABoolean(with parameters) {
// code for functionCallHereThatReturnsABoolean
} else if anotherFunctionCallHereThatReturnsABoolean(with parameters) {
// code for anotherFunctionCallHereThatReturnsABoolean
} else {
// code for else
}


have I missed something ?

The when keyword?

switch (true)
{
case bool when (functionCallHereThatReturnsABoolean(with parameters)==true):
// code for functionCallHereThatReturnsABoolean
break;

case bool when (anotherFunctionCallHereThatReturnsABoolean(with parameters)==true):
// code for functionCallHereThatReturnsABoolean
break;
...
}

AH I wasnt sure that applied to the way I was trying
I read the docs (OMG!) and it wasnt entirely clear how I could/should use it

I’ll give that a go

 {   switch(col)
        {   case 0: return row;
            case 1:  return car.on(car.stops());
            default: return "";   }  
        }

this is how it’s done with Java, you can do similar with booleans

Yeah that part I get
Trouble is my case the values I’m comparing to are computed
So I needed to compare a local variable to a computed value

switch (col)
{
   case computeOneValue() :
   case computeTwoValue() :
}

This form was handy since I could easily add more computations rather than have to, by hand, sort out the constants by hand ahead of time

I had to look that up…

1 Like

In the examples in the given link, you can use Or instead of ||. Is this the only place you can use Or, or is this a new language feature?

Turns out you can only use Or, And, Not in pattern matching, and there’s a reason for it … it’s different from || and && : C# 9: New `and`, `or`, and `not` Keywords for Pattern Matching

Sometimes I think C# is getting a little TOO much love; it’s starting to feel like a kitchen sink approach to language development at times.

You dont HAVE to use all the language features you know :stuck_out_tongue_closed_eyes:

1 Like

And I don’t. But others on my team might (right now I have the luxury of being a “team” of one but that won’t last forever). And then I have to figure out WTF they are on about.

A cleaner approach would see more uptake from folks like me. For example I have some existing classes that cache List<T>s in memory and those T’s are just classes with public properties (mostly strings). I’d consider making them records rather than classes but you try to read up on records and they are read-only except when they aren’t, have a number of other options without much discussion of the tradeoffs, and no clear use case is made for the change (in fairness this may have to do more with the writing chops of the articles I landed on than with records being a bad idea). I’m guessing that under the hood, knowing they are records allows certain optimizations, especially when read-only, but there’s such a welter of options, most of which overlap with existing capability of classes and structs, that I end up saying, screw it, it works really well, don’t break what ain’t fixed.

Another example is the (relatively) new stricter-by-default handling of nulls. The compiler warnings try to catch every conceivable situation in which you might possibly be surprised by nulls but in my experience, all it does is make it impossible to write code. For example, I’ve literally had this happen:

SqlDataReader reader = [code here to get back a DataReader];
string foo = reader["SomeColumn"].ToString();

… and this gets flagged as a no-no due to possible null assignment to foo, when in fact ToString() converts boxed DBNulls to empty strings on its own. So in this case it’s not only overly-sensitive, but pointless and/or stupid, or at best over-cautious because that’s a potentially changeable implementation detail (in theory).

I ended up silencing all those warnings.

That said, I certainly use bits and pieces where it actually simplifies decisions or designs or gives performance gains. I use Span<T> / ReadOnlySpan<T> extensively in string manipulation for example, and those are new since, IIRC, .NET 5. They are like type-safe Xojo Memory abstractions. For strings, they give you access to the underlying char array of the string; it’s still immutable but you can copy to and from char arrays and then do a new String(char array) call. Great for avoiding ephemeral allocations when you need to assemble a new string from bits and pieces of other strings on hot paths. Or if you want to be closer to the metal, you have Memory and ReadOnlyMemory as well.