I can't figure out how to make a switch expression yield a ref
value.
bool cond = true;
int a = 1, b = 2;
// This works
ref int c = ref cond ? ref a : ref b;
// But using a switch expression fails to compile.
// Error CS1525 Invalid expression term 'ref'
c = ref (cond switch { true => ref a, false => ref b });
Am I getting the syntax wrong? Is this even possible?
It doesn't compile regardless of whether I include the outer ref ( )
part. I used a bool
only to quickly illustrate the question, but my actual use case is not so simple.