title: C# .NET - Garbled character when writing 56623 with StreamWriter
body: <p>I have an issue with writing the character <em>56623</em> to a stream using a StreamWriter in UTF16 (the issue persists in other encodings as well). If I get the buffer from the stream, it contains the value <em>65533</em> instead of what I originally wrote. This issue snuck up on me when doing randomised unit tests and it does does not appear for value <em>60000</em> nor <em>95</em>.</p>
  
  <p>To illustrate, I have a minimal program to check the behaviour:</p>
  
  <pre><code>   char value = (char)56623;
     MemoryStream stream = new MemoryStream();
     StreamWriter writer = new StreamWriter(stream, Encoding.Unicode);
     writer.Write(value);
     writer.Close();
  
     var byteArray = BitConverter.GetBytes(value); // Reference bytes
     var buffer = writer.GetBuffer();
  </code></pre>
  
  <p>By reading byteArray and buffer I get:</p>
  
  <pre><code>   byteArray = [221,47] = 11011101 00101111 = 56623
     buffer = [255,254,253,255,...] = BOM 11111101 11111111 ... = BOM 65533
  </code></pre>
  
  <p>Thus, the written value <em>65533</em> is clearly <strong>not equal</strong> to the original <em>56623</em>. However, when trying with the value <em>60000</em> the <strong>correct</strong> values are written:</p>
  
  <pre><code>   byteArray = [96,234] = 01100000 11101010 = 60000
     buffer = [255,254,96,234,...] = BOM 01100000 11101010 ... = BOM 60000
  </code></pre>
  
  <p>I fail to understand why this is the behaviour, but I am unwilling to think that there is an issue with the implementation of StreamWriter so there has the be something I am missing.</p>
  
  <p><strong>What is it that I am not seeing here?</strong></p>
  
  <p>Thank you!</p>