1

I am just wondering whats the most effecient way to break out of case if statement in C#?

case UserA:
{
    if (ConfigurationHelper.GetValue("Environment") == "DEV")
    {
        //upload document for Dev env         
    }
    else if (ConfigurationHelper.GetValue("Environment") == "UAT")
    {
        //upload document for UAT env                            
    }

Would it be the following? or is there a better way to do this?

case UserA:
{
    if (ConfigurationHelper.GetValue("Environment") == "DEV")
    {
        //upload document for Dev env 
        break;
    }
    else if (ConfigurationHelper.GetValue("Environment") == "UAT")
    {
        //upload document for UAT env   
        break;                         
    }
Rafalon
  • 4,450
  • 2
  • 16
  • 30
IOF
  • 251
  • 6
  • 18
  • 9
    You shouldn't need to break out of an if statement. It should finish scope normally or it's written wrong. That's what `else` and `else if` are for. – Zer0 Jan 26 '21 at 08:33
  • 1
    Adding to @Zer0's comment, you would only need to 'break' out of loops(for, while, do-while) or for each switch cases. – Amateur_coder Jan 26 '21 at 08:39
  • 1
    `break` _after_ both `if` and `else if` scope ends. As you would any other `case`. – Fildor Jan 26 '21 at 08:39
  • Please create function for your `if` and `else` and use return. this is more elegance than this construction. – hsd Jan 26 '21 at 08:42
  • 1
    Possibly an [X/Y-Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and your solution may be found here: [Why Switch/Case and not If/Else If?](https://stackoverflow.com/questions/1028437/why-switch-case-and-not-if-else-if) – Chrᴉz remembers Monica Jan 26 '21 at 08:46
  • @ChrᴉzsupportsMonica That's a C++ question. So while the accepted answer _may_ also partly apply to C#, some of the details probably differ. (Just to take that into account) – Fildor Jan 26 '21 at 08:57

3 Answers3

3

You don't need to break if... else if statement. Only one block will execute of if...else if block irrespective of how many blocks being used.

case UserA:
                        
         if (ConfigurationHelper.GetValue("Environment") == "DEV")
         {
              //upload document for Dev env 
         }                                  
         else if (ConfigurationHelper.GetValue("Environment") == "UAT")
         {
              //upload document for UAT env                         
         }
        break; //you can use break at the end of the if...else if blocks


You can use break inside a loop, case statement. Here's the structure of switch case.

switch(expression) 
{
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
    break;
}

Places where break can't be used.[Modified]

namespace Test
{
    class TestClass{         
        static void Main(string[] args)
        {
            int a=5;
            if(a==1)
            {
               //do something.
               //here break can't be used. actually, there's no need to use break. 
               //as only one block will be executed of the if...else if...else blocks
            }
            else
            {
               //do something
            }
        }
    }
}
  • 1
    Last example: I am pretty sure, the compiler will optimize the if / else away, anyway. Hardcoding like this will lead to factually unreachable code. – Fildor Jan 26 '21 at 08:49
1

whats the most effecient way

I can't believe nobody suggested the most obvious solution. So, let me do it. This is how one can handle different environments in C# with zero code:

  1. Create a file named appsettings.X.json where X equals the value of ASPNETCORE_ENVIRONMENT environment variable.
  2. Add environment-specific configurations there.
  3. Done! Your ConfigurationHelper will automatically extract values from a correct file.

Of course, this method has some limitations (like, you cannot use both configurations at the same time). Hope it works for you.

Hirasawa Yui
  • 1,138
  • 11
  • 29
  • 1
    While it's good advice, it doesn't really answer the question. If we ignore the detail and just answer the question "where to put the 'break'?" ... but +1 anyway because I think it's what OP should do. – Fildor Jan 26 '21 at 09:27
0
case UserA:
{
   switch(ConfigurationHelper.GetValue("Environment"))
   {
        case "DEV":
             //upload document for Dev env 
             break;
        case "UAT":
             //upload document for UAT env   
             break;
        default:
             break;
   }
}
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17