title: How do I ignore the UTF-8 Byte Order Marker in String comparisons?
body: <p>I'm having a problem comparing strings in a Unit Test in C# 4.0 using Visual Studio 2010. This same test case works properly in Visual Studio 2008 (with C# 3.5).</p>
  
  <p>Here's the relevant code snippet:</p>
  
  <pre><code>byte[] rawData = GetData();
  string data = Encoding.UTF8.GetString(rawData);
  
  Assert.AreEqual("Constant", data, false, CultureInfo.InvariantCulture);
  </code></pre>
  
  <p>While debugging this test, the <code>data</code> string appears to the naked eye to contain exactly the same string as the literal. When I called <code>data.ToCharArray()</code>, I noticed that the first byte of the string <code>data</code> is the value <code>65279</code> which is the UTF-8 Byte Order Marker. What I don't understand is why <code>Encoding.UTF8.GetString()</code> keeps this byte around.</p>
  
  <p>How do I get <code>Encoding.UTF8.GetString()</code> to <em>not</em> put the Byte Order Marker in the resulting string?</p>
  
  <p><strong>Update:</strong> The problem was that <code>GetData()</code>, which reads a file from disk, reads the data from the file using <code>FileStream.readbytes()</code>. I corrected this by using a <code>StreamReader</code> and converting the string to bytes using <code>Encoding.UTF8.GetBytes()</code>, which is what it should've been doing in the first place! Thanks for all the help.</p>