Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error string encoding (Windows 10 + Visual Studio 2015 + Net 4.6)

My code:

Keys = new Dictionary<string, string>();
Keys.Add("Набег_0", "raid_0");

When I get Keys.ElementAt(0), I have this: {[Íàáåã_0, raid_0]}. Of course, when I run the program, key = "Набег_0" is not defined and the program crashes with a System.Collections.Generic.KeyNotFoundException

This code worked fine when I had Windows 8.1 + Visual Studio 2013 + net 3.5

How do I fix this?

like image 510
lantsev1981 Avatar asked Aug 01 '15 13:08

lantsev1981


Video Answer


1 Answers

You somehow convinced the C# compiler that your source code was written in code page 1251, the default system code page in Eastern Europe and Russia. That's usually caused by the text file missing the utf-8 BOM. Unclear how this happened, maybe you created the file with a text editor other than the one built into Visual Studio. Maybe it got mangled by source control, the ones with a Unix background tend to drop the BOM.

Open the source file in Visual Studio and ensure it still reads correctly. Then use File > Save As, click the arrow on the Save button, select "with encoding" and pick "Unicode (UTF-8 with signature)".

Also make sure that the default is still good. File > Advanced Save Options > change the Encoding if necessary. If you habitually use another text editor then you'll want configure it so it saves files with a BOM.

like image 79
Hans Passant Avatar answered Sep 23 '22 01:09

Hans Passant