title: Why does casting from byte to sbyte give a wrong value for optimized code?
body: <p>The problem can be reproduced with the following code sample, having NUnit 3 installed.</p>
  
  <pre><code>[TestFixture]
  public class SByteFixture
  {
      [Test]
      public void Test()
      {
          var data = new byte[] { 0xFF };
  
          sbyte x = -128;
          data[0] = (byte) x;
          byte b1 = data[0];
          var b2 = (sbyte) b1;
          Assert.AreEqual(b1.ToString(), "128");
          Assert.AreEqual(b2.ToString(), "-128");
      }
  }
  </code></pre>
  
  <ol>
  <li>The project should be a <strong><em>class library</em></strong> because in a <strong><em>console application</em></strong> it's not reproducible.</li>
  <li><p>Should have optimization enabled, i.e. the following setting in the csproj file:</p>
  
  <pre><code>&lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "&gt;
      &lt;Optimize&gt;true&lt;/Optimize&gt;
  &lt;/PropertyGroup&gt;
  </code></pre></li>
  </ol>
  
  <p>The test <strong>passes</strong> when <strong>Optimize is false</strong>, but it <strong>fails</strong> when <strong>Optimize is true</strong> (<code>b2.ToString()</code> gives <code>"128"</code>).</p>
  
  <p>This can be seen using <strong>ReSharper</strong> for running the test or <strong>NUnitConsole</strong>, not reproducible with <strong>VS Test Explorer</strong>.</p>
  
  <p>How can this be explained?</p>