2

I am looking at this code:

public enum MO
{
    Learn, Practice, Quiz
}

public enum CC
{ 
    H
}

public class SomeSettings
{
    public MO Mode { get; set; }
    public CC Cc { get; set; }
}

static void Main(string[] args)
{
    var Settings = new SomeSettings() { Cc = CC.H, Mode = MO.Practice };

    var (msg,isCC,upd) = Settings.Mode switch {
        case MO.Learn => ("Use this mode when you are first learning the phrases and their meanings.",
                          Settings.Cc == CC.H,
                          false),
        case MO.Practice => ("Use this mode to help you memorize the phrases and their meanings.",
                          Settings.Cc == CC.H,
                          false),
        case MO.Quiz => ("Use this mode to run a self marked test.",
                          Settings.Cc == CC.H,
                          true);
        _ => default;
    }
}

Unfortunately it seems that the msg, isCC and upd are not declared correctly and it gives a message saying this:

Cannot infer the type of implicitly-typed deconstruction variable 'msg' and the same for isCC and upd.

Can you help explain to me how I can declare these?

GSerg
  • 76,472
  • 17
  • 159
  • 346
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 1
    is there an online C# 8 compiler to try this? – bolov Nov 23 '19 at 14:54
  • 5
    Could you reproduce this in a [mcve]? That will make it easier to help you, rather than each individual potential answerer having to do the same work to reproduce it. – Jon Skeet Nov 23 '19 at 14:56
  • Can't compile the sample, reproducible code is needed – Pavel Anikhouski Nov 23 '19 at 15:02
  • 4
    @PavelAnikhouski The whole point of the question is the OP is getting a compilation error. – Tanveer Badar Nov 23 '19 at 15:14
  • 1
    Is there any documentation on what that `default` keyword does? The "What's new" page doesn't say anything about it and I'm wondering if that's the issue. – JLRishe Nov 23 '19 at 15:15
  • @JLRishe https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/default#default-literal – GSerg Nov 23 '19 at 15:34
  • 1
    Does this question is result of answers [here](https://stackoverflow.com/questions/58998500/is-there-a-way-for-switch-to-return-a-string-value-using-c-sharp-8-switch-expres)? – Pavel Anikhouski Nov 23 '19 at 16:46
  • A C#8 switch expression doesn't use "case": https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#switch-expressions – Hans Kesting Nov 28 '19 at 10:07

2 Answers2

5

case labels are not used with switch expressions, you have a ; in the middle, and no ; after:

var (msg, isCC, upd) = Settings.Mode switch {
    MO.Learn => ("Use this mode when you are first learning the phrases and their meanings.",
                        Settings.Cc == CC.H,
                        false),
    MO.Practice => ("Use this mode to help you memorize the phrases and their meanings.",
                        Settings.Cc == CC.H,
                        false),
    MO.Quiz => ("Use this mode to run a self marked test.",
                        Settings.Cc == CC.H,
                        true),
    _ => default
};
GSerg
  • 76,472
  • 17
  • 159
  • 346
0

I am writing without checking, but you might try something like this:

(string msg, bool isCC, bool upd) result = Settings.Mode switch ... <rest of your code>

Then use it like this:

result.msg
GrayCat
  • 1,749
  • 3
  • 19
  • 30