C
C#2y ago
Rhythmic

❔ Using compact switch syntax

Is it possible to set format_code = 1 for case _ using C#8.0 switch syntax? // int format_code string format_name = format_code switch { 0 => "Format8Bits", 1 => "Format16Bits", 2 => "ImaAdpcm", _ => "UNKNOWN (trying to interpret as Format16Bits)" };
5 Replies
Angius
Angius2y ago
It's "switch expression", by the by, if you ever need to google it And no, no straightforward way to do that You could use a workaround like
var (formatName, code) = formatCode switch {
0 => ("Format8Bits", formatCode),
1 => ("Format16Bits", formatCode),
2 => ("ImaAdpcm", formatCode),
_ => ("UNKNOWN (trying to interpret as Format16Bits)", 1)
};
formatCode = code;
var (formatName, code) = formatCode switch {
0 => ("Format8Bits", formatCode),
1 => ("Format16Bits", formatCode),
2 => ("ImaAdpcm", formatCode),
_ => ("UNKNOWN (trying to interpret as Format16Bits)", 1)
};
formatCode = code;
Rhythmic
RhythmicOP2y ago
I like that approach, will give it a go. Didn't know you could assign multiple values with a switch expression
Angius
Angius2y ago
It uses tuples and tuple deconstruction
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
var tup = (67, "hello");
var (num, msg) = tup;

Console.WriteLine($"num: {num}, msg: {msg}");
var tup = (67, "hello");
var (num, msg) = tup;

Console.WriteLine($"num: {num}, msg: {msg}");
Console Output
num: 67, msg: hello
num: 67, msg: hello
Compile: 629.874ms | Execution: 46.902ms | React with ❌ to remove this embed.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server