Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get an CS1056 Unexpected character '' on this code

Tags:

c#

I'm getting this unexpected character '' error and I don't understand why.

var list = new List<MyModel>();    
list.Add(new MyModel() {
    variable1 = 942,
    variable2 = 2001,
    variable3 = "my text",
    variable4   = 123
​}); // CS1056 Unexpected character ''  on this line
like image 749
RedHawk Avatar asked Jul 23 '15 02:07

RedHawk


3 Answers

From what the error says and the actual error code I got from an Online compiler after copy/pasting, Your code on this line contains a character that is not visible but that the compiler is trying to interpret. Simply try erase every character starting at your closing bracket towards your number 3 and press Enter again It should be working (it did work for me)

like image 73
Daneau Avatar answered Nov 10 '22 21:11

Daneau


I just deleted the file Version=v4.0.AssemblyAttributes.cs(1,1,1,1) located in my temp folder C:\Users\MyUser\AppData\Local\Temp and then it works perfectly.

For .NET Core you have to delete .NETCoreApp,Version=v2.1.AssemblyAttributes.cs

File to delete

like image 26
Guilhermlimma Avatar answered Nov 10 '22 20:11

Guilhermlimma


As mentioned by Daneau in the accepted answer, the problem is by a character that is not visible in the IDE.

Here are several solutions to find the invisible character with Notepad++.

Solutions 1: Show Symbol

  • Copy the code to Notepad++,
  • Select View -> Show Symbol -> Show All Characters

This can show invisible control characters.

Solutions 2: Convert to ANSI

  • Copy the code to Notepad++,
  • Select Encoding- > Convert to ANSI

This will convert the invisible character to ? if it is a none ANSI character.

Solutions 3: Remove none ASCII characters

  • Copy the code to Notepad++,
  • Open the Find window (Ctrl+F)
  • Select the Replace tab
  • in "Find what" write: [^\x00-\x7F]
  • Leave "Replace with" empty
  • In "Search Mode" select "Regular expression"
  • Find and remove the none ASCII characters

This will remove none ASCII characters.

Note: This can remove valid non ASCII characters (in strings and comments) so try to skip those if you have any.

Tip: Use HEX-Editor plugin

Use Notepad++ HEX-Editor plugin to see the binary code of text. Any character out of the range of 0x00 - 0x7F (0 - 127) is a non ASCII character and a suspect of being the problem.

like image 16
Eliahu Aaron Avatar answered Nov 10 '22 22:11

Eliahu Aaron