switch-expression

Multiple statements in a switch expression: C# 8

橙三吉。 提交于 2020-12-08 06:53:11
问题 Switch expressions were introduced in C# 8. There's plenty of places in codebases, which may be rewritten in this new style. For example, I have some code, which is used for parsing packets from a stream of bytes: switch (command) { case Command.C1: return new P1(); case Command.C2: return new P2(); default: stream.Position++; return null; } The problem is - it can't be converted to a switch expression like return command switch { Command.C1 => new P1(), Command.C3 => new P2(), _ => { stream

C# 8 switch expression: Handle multiple cases at once?

℡╲_俬逩灬. 提交于 2020-04-16 19:27:10
问题 C# 8 introduced pattern matching, and I already found good places to use it, like this one: private static GameType UpdateGameType(GameType gameType) { switch (gameType) { case GameType.RoyalBattleLegacy: case GameType.RoyalBattleNew: return GameType.RoyalBattle; case GameType.FfaLegacy: case GameType.FfaNew: return GameType.Ffa; default: return gameType; } } which then becomes private static GameType UpdateGameType(GameType gameType) => gameType switch { GameType.RoyalBattleLegacy =>

Is there a way for switch to return a string value using C# 8 switch expressions?

我与影子孤独终老i 提交于 2020-01-05 08:27:36
问题 I have this code where each part of the switch returns a value to ModeMessage2 . Is it possible using the new C# switch expressions (or any other code optimization) to optimize the way this switch works? switch (Settings.Mode) { case MO.Learn: ModeMessage2 = "Use this mode when you are first learning the phrases and their meanings."; if (Settings.Cc == CC.H) { Settings.Cc = CC.JLPT5; App.cardSetWithWordCount = null; App.DB.RemoveSelected(); } break; case MO.Practice: ModeMessage2 = "Use this

C# how to use enum with switch

杀马特。学长 韩版系。学妹 提交于 2019-12-28 11:42:11
问题 I can't figure out how to use switches in combination with an enum. Could you please tell me what I'm doing wrong, and how to fix it? I have to use an enum to make a basic calculator. public enum Operator { PLUS, MINUS, MULTIPLY, DIVIDE } public double Calculate(int left, int right, Operator op) { int i = (int) op; switch(i) { case 0: { return left + right; } case 1: { return left - right; } case 2: { return left * right; } case 3: { return left / right; } default: { return 0.0; } } } The end

How to deal with optional arguments when wanting to enable nullable reference types?

白昼怎懂夜的黑 提交于 2019-12-25 01:35:48
问题 I see the very great advantage of turning on (non-)nullable reference types, but I have quite a lot of methods with optional parameters and I am wondering what the right way is to correct the warnings yielded by the compiler in a wise way. Making the parameter nullable by annotating the type with "?" takes all the goodness away. Another idea is to turn all methods with optional parameters into separate methods which is quite a lot of work or yields high complexity (exponential explosion of

c# 8 switch expression multiple cases with same result

好久不见. 提交于 2019-12-18 05:45:12
问题 How does a switch-expression needs to be written to support multiple cases returning the same result? With c# prior to version 8 a switch may be written like so: var switchValue = 3; var resultText = string.Empty; switch (switchValue) { case 1: case 2: case 3: resultText = "one to three"; break; case 4: resultText = "four"; break; case 5: resultText = "five"; break; default: resultText = "unkown"; break; } When I am using the c# version 8 expression syntax its like so: var switchValue = 3;