How to make a switch in C #?

4

I see a lot of people saying it's wrong to switch in the code because it ends up weighing and it's ugly.

Is it correct the way I'm using it and what would be an alternative to using it? Fill in the code for if / else ?

      int TipoOsFiltrada = 0;

      switch (TipoOsFiltrada)
      {
        case 0:
          field = "nome";
          fieldValue = "'" + usuarioLogado.NOME + "'";
          break;
        case 1:
          field = "codcar";
          fieldValue = codCar.ToString();
          break;
        case 2:
          field = "codset";
          fieldValue = codSet.ToString();
          break;
        default:
          field = "nome";
          fieldValue = "'" + usuarioLogado.NOME + "'";
          break;
      }
    
asked by anonymous 29.11.2018 / 12:06

2 answers

8
  

I see a lot of people saying that it is wrong to make a switch in the code because it ends up weighing and it is ugly

I do not see this. It may be that I use good programmers as a reference:)

switch is elegant and fast, so every modern language, like the old ones, has it. Knowing how to use it is very powerful and facilitates a lot, besides being faster than other options in most scenarios. In fact it exists for optimization of execution in the first place, and elegance in the second, so I can not imagine where these ideas come from.

Well, I even imagine it in another scenario. There are some people who say that because they are less object oriented. But if you ask why it's bad, they generally do not know how to say, they read somewhere and they keep repeating. They do not give context, as it happens with those who have incomplete understanding of things (something you are seeking to get rid of here). Of course in some context it is not so suitable to use it, but this goes for all the features of the programming language, without exception, is valid for variables, literals, operators, all commands, even a simple semicolon. >

If the comparison is with if I would say that it is usually more elegant in cases where it can be used (not all it can). The case shown in the question is pretty basic and the most ideal for your adoption. It gets faster and more beautiful, although pretty is something subjective (it would be more if you did not have to use break ).

Of course, considering the first line, it does not even make sense because it is guaranteed that only the first line will run.

You have a question that talks more about how it works: How does the switch work under the covers? ( although it is another language, works the same)

It may also be useful to read:

I did not even mention its use as pattern matching which is much more powerful and interesting, and is available in C #, although many do not know it. And version 8 can be used as expression .

    
29.11.2018 / 12:20
3

The issue of using the Switch / Case can not be ruled between leaving the code beautiful or ugly, but whether it is valid or not.

I see switch / case being commonly used to do the assignment of environment variables for example. Your code example fits this premise because it deals with a pre stipulated filter.

    
29.11.2018 / 12:18