2016-01-22 24 views
9

Sto osservando un comportamento strano quando si utilizza una dichiarazione di commutazione nullable lungo nell'aggiornamento 1 VS2015 che non vedo in altre versioni di Visual Studio in cui viene eseguita come previsto.Nullable L'istruzione switch lungo non produce l'output previsto in VS2015

class Program 
{ 
    static void Main(string[] args) 
    { 
     NullableTest(-1); 
     NullableTest(0); 
     NullableTest(1); 
     NullableTest(2); 
     NullableTest(null); 
    } 

    public static void NullableTest(long? input) 
    { 
     string switch1; 
     switch (input) 
     { 
      case 0: 
       switch1 = "0"; 
       break; 
      case 1: 
       switch1 = "1"; 
       break; 
      default: 
       switch1 = "d"; 
       break; 
     } 

     string switch2; 
     switch (input) 
     { 
      case -1: 
       switch2 = "-1"; 
       break; 
      case 0: 
       switch2 = "0"; 
       break; 
      case 1: 
       switch2 = "1"; 
       break; 
      default: 
       switch2 = "d"; 
       break; 
     } 

     string ifElse; 
     if (input == 0) 
     { 
      ifElse = "0"; 
     } 
     else if (input == 1) 
     { 
      ifElse = "1"; 
     } 
     else 
     { 
      ifElse = "d"; 
     } 
     Console.WriteLine("Input = {0}, Switch 1 output = {1}, Switch 2 output = {2}, If Else = {3}", input, switch1, switch2, ifElse); 
    } 

codice Questo esempio produce il seguente output (Allineati per migliorare la leggibilità):

Input = -1, Switch 1 output = d, Switch 2 output = d, If Else = d 
Input = 0, Switch 1 output = 0, Switch 2 output = d, If Else = 0 
Input = 1, Switch 1 output = d, Switch 2 output = d, If Else = 1 
Input = 2, Switch 1 output = d, Switch 2 output = d, If Else = d 
Input = , Switch 1 output = d, Switch 2 output = d, If Else = d 

sto solo osservando questo comportamento con i tipi Nullable. I tipi non annullabili funzionano come previsto.

Si noti che questo è non lo stesso comportamento che è in this question, e non è causata da this bug del compilatore che è stato risolto in VS2015 Update 1. Ho verificato che entrambi questi esempi funzionare correttamente.

risposta

2

Sembra che questo sia causato da this bug ed è stato risolto con this pull request.

Ho provato una recente build di Roslyn e il codice di esempio nella domanda funziona come previsto ora.

Here è la versione aggiornata di MSBuild Tools 2015 che ha risolto il problema.

1

Questo è un bug. Credo che debba essere rimasto lo stesso bug. È meglio sostituire il codice con uno If per evitare comportamenti imprevisti.