37

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
RedHawk
  • 383
  • 1
  • 3
  • 4

9 Answers9

58

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)

Daneau
  • 1,085
  • 9
  • 15
  • 1
    Interesting. My VS/Resharper/something else removed that when I pasted it in the VS. Good catch. – jdphenix Jul 23 '15 at 02:26
  • 2
    @GrantWinney A "zero width space" - https://en.wikipedia.org/wiki/Zero-width_space may explain why it ended up in code if it was from the interwebz – jdphenix Jul 23 '15 at 02:44
  • @Daneau, thanks for your help. I re-typed everything which solved this error message. Can kick myself for overlooking something so simple and keep thinking there must be something wrong with the syntax. ;-) – RedHawk Jul 23 '15 at 21:04
  • Copy your code and paste it on MS Word. Then you will see the bad character. – Koray Mar 10 '17 at 10:03
  • I deleted the `obj` folder of the project and then everything compiled successfully. The reported file for me was `NetStandard[something].cs` but no file like that was present in either the AppData\Local\Temp directory nor in the `obj` folder. Not sure what it was. – alelom Jul 20 '22 at 19:49
53

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

Antoine V
  • 6,998
  • 2
  • 11
  • 34
Guilhermlimma
  • 643
  • 5
  • 13
  • 3
    You need to delete the file that matches your .NET version e.g. `.NETFramework,Version=v4.5.1.AssemblyAttributes`, `.NETFramework,Version=v4.6.1.AssemblyAttributes` – Chris Halcrow Nov 14 '18 at 01:09
  • 6
    Just had the same issue. For me, the file to delete was in the `obj` folder of the project – Kristoffer Lerbæk Pedersen Jul 04 '21 at 23:42
  • 1
    Confirm that I deleted the `obj` folder of the project and then everything compiled successfully. The reported file for me was `NetStandard[something].cs` but no file like that was present in either the AppData\Local\Temp directory nor in the `obj` folder. Not sure what it was. – alelom Jul 20 '22 at 19:48
18

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.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
  • Solution 2 fixed it for me. I suspect the issue appeared as a result from some git action I did in Visual Studio Code. Something blinked in the code, and I don't know what exactly happened but I had a nagging feeling something had changed without me knowing what exactly. Then when I ran it in the browser, this error was there. – awe Sep 14 '21 at 09:17
  • 1
    Visual Studio also has a built in hex editor. Use "Open With..." (either from right-clicking the file in the solution explorer; or from the Open dialog (Ctrl+O) clicking the small triangle on the Open button), and then pick **Binary editor**. – Jeppe Stig Nielsen Nov 11 '21 at 12:29
  • I deleted the `obj` folder of the project and then everything compiled successfully. The reported file for me was `NetStandard[something].cs` but no file like that was present in either the AppData\Local\Temp directory nor in the `obj` folder. Not sure what it was. – alelom Jul 20 '22 at 19:49
2

Just reporting my direct experience. As Daneau wrote, I had a character (ASCII DLE, I copied while messing up a zebra printer) hiding in the text. I could not afford to rewrite everything, so I used notepad++ "View->Show Symbol->Show All Characters" feature. I apologize for not commenting Daneau entry, but I don't have enough reputation.

FlavioG
  • 41
  • 1
  • 2
2

go to C:\Users\UserName\AppData\Local\Temp\ and clear the data or remove the file specified in the error, that will solve the issue.

VS will add the required file on auto, no worries.

sami ullah
  • 925
  • 1
  • 9
  • 15
2

I got this error when I moved my application from one folder to another, I resolved this by deleting the Debug folder inside the obj folder.

user1947393
  • 153
  • 4
  • 18
1

Write the code again without copying it. That worked for me

0

It indeed has to do with copy pasting code and characters that you cannot see. The easiest way to fix it is by passing your copy pasted code into a note application or simple text program which will automatically remove these invisible characters. After that simply copy the code from the text editor and paste it into your IDE.

0

For some reason this happened to me on every project in my solution. My fix was to delete all bin and obj folders in my solution.

DLeh
  • 23,806
  • 16
  • 84
  • 128