-4

I'm refining my understanding of arrays and if / else if / else statements. I want to see if I can convert my if / else if / else statements into Switch / Case. What's the best way to go about this? I have heard that Switch / Case is much more efficient when dealing with a broader range of choices. Should I stay with the if / else? or is there a way to convert my choices into Switch Case? Here is the example I am making for them and in the code.

{
        int []  acct = new int [3];

        acct[0] = 8675309;
        acct[1] = 8675310;
        acct[2] = 8675311;

        Console.WriteLine("Enter your account number");
        int myacct = int.Parse(Console.ReadLine());
        //int myacct = Convert.ToInt32(Console.ReadLine());<--This works too
        if (myacct == acct[0])
        {
            Console.WriteLine("What is your name?");
        }
        else if (myacct == acct[1])
        {
            Console.WriteLine("What is your name?");
        }
        else if (myacct == acct[2])
        {
            Console.WriteLine("What is your name?");
        }
        else
        {
            Console.WriteLine("Sorry you don't have access");
        }

        string name = Console.ReadLine();

        string[] names = new string[3] { "Jenny", "Roberto", "Sally" };
        /*  This shows them how to tighten the code up compaired to the technique we used above
        names[0] = "Jenny";
        names[1] = "Roberto";
        names[2] = "Sally";
        */

        //I'd like to make the following code into a Switch and Case type statement instead of using else if.


        /*
        if (myacct == acct [0] && name == "Jenny")
        {
            Console.WriteLine("Welcome "+names[0] + "!");
        }


        else if (myacct == acct[1] && name == "Roberto")
        {
            Console.WriteLine("Welcome Roberto" + names[1] + "!");
        }
        else if (myacct == acct[2] && name == "Sally")
        {
            Console.WriteLine("Welcome Sally" + names[2] + "!");
        }
        else
        {
            Console.WriteLine("Account number and Names do not match");
        }
        */
    }
}
Conley Rasor
  • 51
  • 1
  • 6
  • 2
    What exactly is your question? its not clear – BugFinder Oct 01 '19 at 11:47
  • 3
    How can you teach someone something that you don't know about? – B001ᛦ Oct 01 '19 at 11:48
  • but you know that switch/case is for constant? – Selvin Oct 01 '19 at 11:49
  • That's why I'm asking. Can I change the if else code into a switch case statement? I'm trying to learn so I can show alternate ways to do the same basic code. – Conley Rasor Oct 01 '19 at 11:49
  • You might want to read through Microsoft's documentation on [C# switch statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch) – crashmstr Oct 01 '19 at 11:50
  • Perhaps [this case-label reference](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch#case-labels) might be helpful? – Some programmer dude Oct 01 '19 at 11:50
  • whats wrong with you guys, trying to solve this and got downvotes... – Exar666Kun Oct 01 '19 at 11:53
  • 1
    The code might not become clearer if you refactor it to using switch but you can do `switch (myacct) { case int a when a == acct[0]: ...` and so on. Example: https://dotnetfiddle.net/WSqXeX - However, you might benefit more from a completely different approach altogether, but it's hard to tell from simple examples. – Lasse V. Karlsen Oct 01 '19 at 12:31
  • Thank you so much Lasse, I think I'm close ```switch (myacct, name) { case int a when a == acct[0]: case string b when b == names[0]: Console.WriteLine("Welcome " + names[0] + "!"); break; }``` – Conley Rasor Oct 01 '19 at 12:38

1 Answers1

0

I look at it this way:

  • switch(value) declares coder's intention to make branching decisions based on value
  • if-else if can have arbitrary conditions
  • there is a possibility of an accidental if instead of else if in the middle of the chain that can lead to either bugs or misunderstanding of the original coder's intent.

Up to C# 6.0 switch's case could only operate with constant values, which means that with a chain of if-else if being more flexible you might have some difficulty directly translating it into a switch in C# 6.0 and below.

Starting with C# 7.0 a more flexible pattern matching was introduced, making it possible to use non-constant patterns.

In C# 8.0 there are also switch expressions, property and tuple patterns that can make the code much more readable and concise in some cases.

DK.
  • 3,173
  • 24
  • 33