1

How to achieve this same below with switch statement,

public static string GetPathWithPrefix(string path)
{
    string pathWithPrefox = string.Empty;
    if(path == ConfigurationManager.AppSettings["path1"])
    {
        pathWithPefix = ConfigurationManager.AppSettings["path1WithPrefix"];
    }
    else if(path == ConfigurationManager.AppSettings["path2"])
    {
        pathWithPefix = ConfigurationManager.AppSettings["path2WithPrefix"];
    }
    else if(path == ConfigurationManager.AppSettings["path3"])
    {
        pathWithPefix = ConfigurationManager.AppSettings["path3WithPrefix"];
    }
    return pathWithPefix;
}

I am getting compilation error when using ConfigurationManager.AppSettings["path1"] in case of switch.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Sara
  • 55
  • 5
  • 5
    You cannot because you are evaluating different things. A `switch` statement is for when you are evaluating different conditions for the same thing. – Fred May 09 '22 at 09:09
  • 1
    Does this answer your question? [Switch case in C# - a constant value is expected](https://stackoverflow.com/questions/7593377/switch-case-in-c-sharp-a-constant-value-is-expected) – mortb May 09 '22 at 09:19

3 Answers3

2
  1. Store the mapping in appropriate data structure
static readonly ImmutableDictionary<string, string> mapping = new Dictionary<string, string>
{
    { "path1", "path1WithPrefix"},
    { "path2", "path2WithPrefix"},
    { "path3", "path3WithPrefix"},
}.ToImmutableDictionary();
  1. Use the data structure with a simple lookup
public static string GetPathWithPrefix(string path)
{
    if (!mapping.ContainsKey(path))
        throw new InvalidOperationException("Provided path is unknown");

    return ConfigurationManager.AppSettings[mapping[path]];
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
1

You need to create const variable for paths and then you can use that veriable in switch case.

follow the example:

public class HelloWorld
{
    public const string path1 = "test1";// ConfigurationManager.AppSettings["path1"];
    public const string path2 = "test2";// ConfigurationManager.AppSettings["path2"];
    public const string path3 = "test3";// ConfigurationManager.AppSettings["path3"];
    public static void Main(string[] args)
    {
        Console.Write(GetPathWithPrefix("test1"));
    }

    public static string GetPathWithPrefix(string path)
    {
            string pathWithPefix = string.Empty; 
            switch(path){
              case path1: 
                pathWithPefix = "t1";//ConfigurationManager.AppSettings["path1WithPrefix"];
                break;
              case path2: 
                pathWithPefix = "t2";//ConfigurationManager.AppSettings["path2WithPrefix"];
                break;
              case path3: 
                pathWithPefix = "t3";//ConfigurationManager.AppSettings["path3WithPrefix"];
                break;
            }
         
            return pathWithPefix;
        }
}
Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41
0

The switch statement is not the same thing as a big if-else statement.

Each case must be unique and evaluated statically. The switch statement does a constant time branch regardless of how many cases you have.

Please check this also. C# switch statement limitations - why?