Identical condition

I am converting some VB.NET code to C #, since I am more comfortable with it, and this helps me solve problems faster. However, I came across this code, which is NOT a bug in VB.NET, but converting it to C # generates a compiler error.

VB.NET Code

Select Case name Case "FSTF" ..... Case "FSTF" ..... End Select 

Converted C # Code

 switch(name) { case "FSTF": ....; break; case "FSTF": ....; break; } 

And the error:

The case of the label "FSTF": "already occurs in this switch statement.

What is the solution here - does this mean that in the VB.NET code the second case statement was just a dummy - or was it the first case of a dummy?

+6
source share
2 answers

From the documentation for Select ... Case :

If testexpression matches the expressionlist clause in more than one Case clause, only the following statements are run following the first match.

So, here the second case is actually redundant. Personally, I prefer the C # approach, highlighting what is almost certainly an inconspicuous programming error, rather than the intentional introduction of a duplicate case ...

+13
source

I assume that this was done in such a way as to make VB.NET compatible with Visual Basic 6.0 and older versions, because this is how they behaved. If VB.NET reported a compilation error, such as C #, then it would be more difficult to port the old Visual Basic code to VB.NET.

The odd thing is that VB.NET does not seem smart enough to rule out a redundant case in the generated CIL . This leads to another peculiar difference between C # and VB.NET. That is, VB.NET does not change its strategy from a sequential search to a Dictionary search when targeting string types as C # . This means that VB.NET Select constructs using string types can run slower than C # switch . The reason for this ( MarkJ credit) is that C # case statements can only contain constants, while case Visual Basic statements can contain expressions.

+2
source

Source: https://habr.com/ru/post/894867/


All Articles