Why do I get an CS1056 Unexpected character '' on this code - 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

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)

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

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.

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.

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

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.

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.

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.

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.

Related

Unity text URL adds extra characters [duplicate]

Wordpress is putting this at the end of my permalink on the live site... %E2%80%8E anyone know why? Thanks guys?!
If you copy post title from MS Word or WordPad or similar editor. This char is like end of line.
Step 1) Identify the link, and open the post or page it appears on in the WordPress Dashboard.
Step 2) We need to delete the invisible character causing the issue, so delete the last several characters from the URL, including the quotation mark, so that this
Step 3) Manually retype what was deleted.
Step 4) Click Update then go and check the revised post to confirm the problem is resolved.
https://www.wpkb.com/fix-wordpress-links-%E2%80%8E-end/
These invisible unicode characters are actually there (unwillingly). You can notice them when moving cursor across them with arrow keys. They use to be added by formatting editors like Word. It's crazy, but Edge adds them even to window title =-O (messing with password managers) or MS Teams Wiki to code snippets (which are meant for preserving space-indented plain text).
It's complicated to get rid of them, because almost all plain text editors and browsers (hence all webapps) today support unicode and even ctrl-shift pasting them preserves them. Even if you try to backspace them, editors usually keep them to preserve rtl/ltr text orientation for you.
Copy the title to some hex editor, remove the characters there and copy it back. Or copy just the ascii part from address bar (if they are URL encoded) and clear the title field by selecting all (ctrl-a).
I use:
PSPad (natively)
Notepad++ (with HEX-Editor plugin)
Common invisible characters:
Code point
UTF-8 hex
Name
U+200B
e2 80 8b
ZERO WIDTH SPACE
U+200E
e2 80 8e
LEFT-TO-RIGHT MARK
`U+200F
e2 80 8f
RIGHT-TO-LEFT MARK
https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128
Yes, If you copied it from some editor.
Simple solution is to just copy the content from editor and paste it in 'notepad' text editor as it doesn't support UTF-8 character.
you will easily notice that buggy characted/text like '%E2%80%8E' in your text.
these are all non-printable ASCII chars
like these are all äÄçÇéÉêöÖÐþúÚ
to remove use this code
function remove_non_ascii(str) {
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace(/[^\x20-\x7E]/g, '');
}
console.log(remove_non_ascii('äÄçÇéÉêHello-WorldöÖÐþúÚ'));
If you use some characters in your link WordPress will show %E2%80%8E instead of those. for example if you use Half Space (CTRL + Space or CTRL + Shift + 2) in your link, WordPress shows %E2%80%8E. solution: just use text + - in your links

Character sequence messes up the order of characters?

I had to handle an exception, by catching it and matching the message and if the message contains a certain error code, do something (not relevant).
The exceptions message is this (in English, but the code and the gibberish after it is the same in any language):
$-5002 - $make sure that the consumed quantity of the component item would not cause the item's stock to fall below zero [ige1.whscode][line: 1] , 'production order no: 20580033 line: 1' [الرسالة 3559-7]
I had to work with the code 7-3559 (as displayed). In my code, I just did a e.Message.Contains("7-3559") and it failed to catch the exception. Wondering what went wrong I copy pasted the error massage to regex101.com and after a bit of trial and error I realized that e.Message.Contains("3559-7") is the real code and it works. I just don't know why. What messes up the string to display it in such a way that 7- is actually -7 and also behind 3559?
I guess I should also mention I am working with Visual Studio 2019 and C#.
Check out the regex here.
HxD:
This is a common issue encountered when using bidirectional text, in other words, a text that contains both texts directionality: Right-to-Left (RTL) such as Arabic texts, and Left-to-Right (LTR).
Here we have the Arabic text mixed with English text so some rules will be applied to the text to determine the directionality. You may find details about this here.
In short, the text you see in the debugger is how the text will appear when you print it but not how it is represented in memory.
Here I use Linqpad to paste the text and the editor has immediately transformed it into the representation in memory. And once printed, the text is shown with a different directionality.

C# Reading files and encoding issue

I've searched everywhere for this answer so hopefully it's not a duplicate. I decided I'm just finally going to ask it here.
I have a file named Program1.exe When I drag that file into Notepad or Notepad++ I get all kinds of random symbols and then some readable text. However, when I try to read this file in C#, I either get inaccurate results, or just a big MZ. I've tried all supported encodings in C#. How can notepad programs read a file like this but I simply can't? I try to convert bytes to string and it doesn't work. I try to directly read line by line and it doesn't work. I've even tried binary and it doesn't work.
Thanks for the help! :)
Reading a binary file as text is a peculiar thing to do, but it is possible. Any of the 8-bit encodings will do it just fine. For example, the code below opens and reads an executable and outputs it to the console.
const string fname = #"C:\mystuff\program.exe";
using (var sw = new StreamReader(fname, Encoding.GetEncoding("windows-1252")))
{
var s = sw.ReadToEnd();
s = s.Replace('\x0', ' '); // replace NUL bytes with spaces
Console.WriteLine(s);
}
The result is very similar to what you'll see in Notepad or Notepad++. The "funny symbols" will differ based on how your console is configured, but you get the idea.
By the way, if you examine the string in the debugger, you're going to see something quite different. Those funny symbols are encoded as C# character escapes. For example, nul bytes (value 0) will display as \0 in the debugger, as NUL in Notepad++, and as spaces on the console or in Notepad. Newlines show up as \r in the debugger, etc.
As I said, reading a binary file as text is pretty peculiar. Unless you're just looking to see if there's human-readable data in the file, I can't imagine why you'd want to do this.
Update
I suspect the reason that all you see in the Windows Forms TextBox is "MZ" is that the Windows textbox control (which is what the TextBox ultimately uses), uses the NUL character as a string terminator, so won't display anything after the first NUL. And the first thing after the "MZ" is a NUL (shows as `\0' in the debugger). You'll have to replace the 0's in the string with spaces. I edited the code example above showing how you'd do that.
The exe is a binary file and if you try to read it as a text file you'll get the effect that you are describing. Try using something like a FileStream instead that does not care about the structure of the file but treats it just as a series of bytes.

How to compare text: ANSI vs Unicode

I'm trying to find a certificate by serial number.
Apparently both sides are equal but debugger says they are not.
What am I missing here? I noticed IDE warned me when I copied & pasted
serial number into text editor, I didn't care about the message;
unicode blah blah...
UPDATE
I copied the serial number from certificate window.
Open Notepad2 new file. It's default ANSI. Pasted the text. Copied again. Pasted into VS. Values are not equal.
Set Notepad2 file as Unicode. Pasted serial number. Copied from Notepad2 and pasted into VS. Now they are equal.
It seems unicode vs ansi problem.
I can't see anything wrong with how you create the string or how you compare it. That should work just fine. Strings in .NET are always unicode, so there is no possible encoding problem either.
Retype the serial string manually. It seems that you got some unusual characters in there when pasting it, for example non-breaking spaces instead of regular spaces.
When I copied I've selected "extra space". Weird but it doesn't appear in the IDE. If I select the serial number carefully, everything will be ok.
In Notepad it appears as below:
?00 c4 aa b9 b1 08 90 5d
It's hex 3F char (question mark) and it doesn't appear in notepad if it's unicode. In ANSI mode it becomes visible as ?
As far as I know, comparison in UNICODE works flawlessly when English alphanumerics are used. For other languages, there are caveats.
Will you please provide "copy-able" code, so that I ca check it on my machine? (PS: I am feeling too lazy to type it..:o)
Also, note that == works only with value types. You should use EqualsTo() for comparing ref types (this way, you will be comparing values, not references!)
Instead of
operator==
you should use
string.Equals(object Obj)
method. The "==" tests for equality of the reference not contents.
See MSDN for further reference.

How to Handle Accented Characters in a Directory Name

I have a problem with using Directory.Exists() on a string that contains an accented character.
This is the directory path: D:\ést_test\scenery. It is coming in as a simple string in a file that I am parsing:
[Area.121]
Title=ést_test
local=D:\AITests\ést_test
Layer=121
Active=FALSE
Required=FALSE
My code is taking the local value and adding \scenery to it. I need to test that this exists (which it does) and am simply using:
if (!Directory.Exists(area.Path))
{
// some handling code
area.AreaIsValid = false;
}
This returns false. It seems that the string handling that I am doing is replacing the accented character. The text visualizer in VS2012 is showing this (directoryManager is just a wrap around System.IO.Directory):
And the warning message as displayed is showing this:
So it seems that the accented character is not being recognized. Searching for this issue does turn up but mostly about removing or replacing the accented character. I am currently using 'normal' string handling. I tried using FileInfo but the path seems to get mangled anyway.
So my first question is how do I get the path stored into a string so that it will pass the Directory.Exists test?
This raises a wider question of non latin characters in path names. I have users all over the world so I can see arabic. Russian, Chinese and so on in paths. How can I handle all of these?
The problem is almost certainly that you're loading the file with the wrong encoding. The fact that it's a filename is irrelevant - the screenshots show that you've lost the relevant data before you call Directory.Exists.
You should make sure you know the file encoding (e.g. UTF-8, Cp1252 etc) and then pass that in as an argument into however you're loading the file (e.g. File.ReadAllText). If this isn't enough information to get you going, you'll need to tell us more about the file (to work out what encoding it's in) and more about your code (how you're reading it).
Once you've managed to load the correct data, I'd hope that the file aspect just handles itself automatically.

Categories