How to Resolve Error "CS1056: Unexpected character 'character'" in C#
The Compiler Error CS1056 is a lexical analysis error. The message reads: "Unexpected character 'character'".
This error occurs when the C# compiler encounters a specific character in your source file that is not part of the C# language specification. C# allows a specific set of characters for keywords, identifiers, and operators. If you introduce characters outside this set—such as invisible control characters, "smart quotes" from word processors, or specific symbols used incorrectly—the compiler cannot parse the file and halts with this error.
This guide identifies the most common sources of these invalid characters and how to remove them.
Understanding Invalid Characters
Valid C# source code consists of:
- Standard ASCII characters (letters, numbers, punctuation like
{,;,+). - Unicode characters in Identifiers (e.g., variable names like
int 数量 = 10;are allowed). - Unicode characters inside Strings and Comments.
Invalid characters generally appear in the structural parts of the code:
- Word processor formatting (Smart Quotes
“instead of"). - Non-breaking spaces (NBSP) where normal spaces are expected.
- Currency symbols or mathematical notation not supported by C#.
Scenario 1: Copy-Paste "Gremlins" (Smart Quotes)
This is the most frequent cause. You copied code from a blog post, a Word document, or a chat app (like Slack/Teams) that auto-formatted the text. These tools often replace standard "straight quotes" with "curly quotes" (smart quotes).
Example of error
public class Program
{
static void Main()
{
// ⛔️ Error CS1056: Unexpected character '“'
// ⛔️ Error CS1056: Unexpected character '”'
// These are not valid string delimiters in C#.
string message = “Hello World”;
}
}
Solution: Use Straight Quotes
Delete the invalid characters and type the quote key on your keyboard (" or ').
public class Program
{
static void Main()
{
// ✅ Correct: Standard ASCII double quotes.
string message = "Hello World";
}
}
Visual Studio Extension: Extensions like "Fix Mixed Tabs" or simply using "Find and Replace" (Ctrl+H) can help you quickly swap “ for ".
Scenario 2: The Dollar Sign ($) Placement
The $ character is used for String Interpolation. However, it must be placed immediately before the opening quote of a string. If you place it elsewhere (e.g., trying to use it as a variable prefix like in PHP or PowerShell), CS1056 occurs.
Example of error
Using $ as part of a variable name or assignment syntax.
public void Process()
{
int cost = 10;
// ⛔️ Error CS1056: Unexpected character '$'
// C# variables cannot start with '$'.
int $price = 20;
// ⛔️ Error CS1056
// Attempting to use PHP-style variable access.
Console.WriteLine($cost);
}
Solution: Correct Usage
Use $ only for string interpolation.
public void Process()
{
int cost = 10;
int price = 20; // Normal variable name
// ✅ Correct: $ is valid here because it prefaces a string "..."
Console.WriteLine($"Cost: {cost}, Price: {price}");
}
Scenario 3: Zero-Width Spaces and Invisible Characters
Sometimes code copied from websites includes hidden formatting characters, such as the Zero Width Space (\u200B) or Non-Breaking Space (\u00A0). These look like normal spaces (or nothing at all) in the editor, but the compiler sees them as illegal symbols.
Example of error
The code looks perfect to the human eye, but fails to build.
// ⛔️ Error CS1056: Unexpected character '' (appears empty)
// There is a hidden character between 'int' and 'id'.
intid = 10;
Solution: Retype or "Paste as Plain Text"
- Delete the line: Delete the whitespace around the error location and press Spacebar again.
- Paste Special: When copying code, use
Ctrl + Shift + V(or Right Click > Paste as Plain Text) to strip formatting. - Hex Editor: If the file is stubborn, open it in a text editor that shows hidden characters (like Notepad++ "Show All Characters") to find the anomaly.
Conclusion
CS1056 means there is "dirt" in your source file.
- Check Quotes: Look for curly quotes (
“”‘’) and replace them with straight quotes ("'). - Check Dashes: Ensure minus signs aren't actually em-dashes (
—) or en-dashes (–). - Retype Whitespace: If the error points to an empty space, delete it and press spacebar to ensure it's a standard ASCII space (32).