Home > Software design >  static Dictionary cannot be used in switch case
static Dictionary cannot be used in switch case

Time:09-16

I have the following switch statement (with this syntax that's new to me).

I also have the following Dictionary / Enum in a static class.

Switch statement :

public static Dictionary<string, string> MyMethod(string brands, int templateIndex, long accountId)
{
   return accountId switch
   {
      AccountsId[AccountLang.French] => Fr.GetContent(templateIndex, brands),
      AccountsId[AccountLang.Spanish] => ES.GetContent(templateIndex, brands),
      _ => throw new Exception($"Account Id not found {typeof(LangHelper)}")
    };
}

Dictionary / Enum:

public static readonly Dictionary<AccountLang, long> AccountsId = new Dictionary<AccountLang, long>()
{
     {AccountLang.French , 25****** },
     {AccountLang.Spanish , 55****** },
};

public enum AccountLang
{
  French,
  Spanish,
}

And here are the 3 errors I have all of them for AccountsId[AccountLang.French] in the switch case:

Error CS0246 The type or namespace name 'AccountsId' could not be found (are you missing a using directive or an assembly reference?)

Error CS8121 An expression of type 'long' cannot be handled by a pattern of type 'AccountsId[]'.

Error CS0270 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

If I the switch case with this syntax I have the same error but on compilation :

long aId when AccountsId[AccountLang.French] == aId => Fr.GetContent(templateIndex, brands)

If I place the namespace and class like this NamespaceName.className.NameAccountsId its the same problem

What I am missing here looks like the first issue comes because it cannot find AccountsId dictionary but if I use it outside he switch case it works perfectly.

CodePudding user response:

You are not using a switch statement but a switch expression. From the documentation:

Each switch expression arm contains a pattern, an optional case guard, the => token, and an expression.

The patterns are listed as

In those constructs, you can match an input expression against any of the following patterns:

  • Declaration pattern: to check the run-time type of an expression and, if a match succeeds, assign an expression result to a declared variable. Introduced in C# 7.0.
  • Type pattern: to check the run-time type of an expression. Introduced in C# 9.0.
  • Constant pattern: to test if an expression result equals a specified constant. Introduced in C# 7.0.
  • Relational patterns: to compare an expression result with a specified constant. Introduced in C# 9.0.
  • Logical patterns: to test if an expression matches a logical combination of patterns. Introduced in C# 9.0.
  • Property pattern: to test if an expression's properties or fields match nested patterns. Introduced in C# 8.0.
  • Positional pattern: to deconstruct an expression result and test if the resulting values match nested patterns. Introduced in C# 8.0.
  • var pattern: to match any expression and assign its result to a declared variable. Introduced in C# 7.0.
  • Discard pattern: to match any expression. Introduced in C# 8.0.

In your example code you are using a runtime value in the place of a pattern. This does not fit into any of the categories above.

Simplest fix would be to change the order of the dictionary, so you make a language lookup from the account id instead. If needed, this language enumeration could then be used in a switch in case you need a way to map between the enumeration and your translations.

  • Related