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
Rhythmic2y 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
More Posts
✅ Using Activator.CreateInstance() when the constructor has arguments - not finding constructorI'm trying to create a class instance dynamically using `Activator.CreateInstance()`, and it's crash❔ Creating a list to loop through school grades & pointsI'm trying to create a list to save my grades & points and loop through them to be able to account t❔ Creating a class that takes a Logger for both the class and its base classI'm trying to figure out the best way to use Loggers in my app. All of the documentation that I've ❔ Basic question on linear search in listHello, I am making a blog/notebook for an assignment and I have a list which contains vectors with 3Generic api base clientI'm trying to implement a generic api base client using `Polly` and `System.Net.Http.Formatting`. I ❔ Finding which port a WebApplication has bound toI have a .NET web application, and I'm telling it that it can pick any port that it likes (by giving❔ Method WriteToTable(); isn't doing anything to the database fileMy code: ```private void button1_Click(object sender, EventArgs e) { WriteToTable(); ✅ Iteration through a list in csprojI have a csproj file with a next section ```xml <Target Name="PublishRunWebpack" AfterTargets="CoAzure AD; The app is trying to access a service that your organization lacks a service principal forI am running into this error after trying to set up authentication in my blazorwasm app: "The app is❔ async code in cshtml file of asp net framework that target IE browserhi all, i understand that Internet Explorer arent' compatible with async/await, but is there an alte