How to Resolve Error "CS1011: Empty character literal" in C#
The Compiler Error CS1011 is a syntax error regarding the char data type. The message reads: "Empty character literal".
In C#, a character literal is denoted by single quotes ('). The char type is designed to hold exactly one Unicode character. Unlike a string (which uses double quotes " and can hold zero characters), a char cannot be empty. Writing '' (two single quotes with nothing in between) is physically impossible for the data type to store, because there is no ASCII or Unicode value for "nothing."
This guide explains the difference between empty strings and characters and how to handle "default" character values.
Understanding char vs string
- String (
"..."): Represents a sequence of characters. A sequence can have a length of 0.""is a valid empty string. - Char (
'...'): Represents a single 16-bit integer value mapping to a Unicode symbol. It must always have a size of 1.
There is no such thing as an empty char, just as there is no such thing as an "empty integer" (an int must be a number like 0, it cannot be void).
Scenario 1: Attempting to Initialize Empty
This commonly happens when a developer initializes variables and wants to put a "placeholder" in a char variable, similar to how they might assign "" to a string.
Example of error
public class User
{
// ⛔️ Error CS1011: Empty character literal
// The compiler sees two single quotes with nothing between them.
public char MiddleInitial = '';
}
Solution: Use a Space or Specific Value
You must provide exactly one character. If you mean "Space", press the spacebar.
public class User
{
// ✅ Correct: Contains a space character (ASCII 32)
public char MiddleInitial = ' ';
// ✅ Correct: Contains a specific letter
public char Grade = 'A';
}
Scenario 2: Confusing Strings with Chars
Sometimes, a developer intends to define an empty string but accidentally uses single quotes instead of double quotes. In C#, single quotes are strictly for char.
Example of error
public void ProcessText()
{
// ⛔️ Error CS1011: Empty character literal
// The developer likely meant to create an empty string.
var prefix = '';
}
Solution: Use Double Quotes
Change the type to string (or var) and use double quotes.
public void ProcessText()
{
// ✅ Correct: An empty string is valid.
string prefix = "";
// OR
string prefix2 = string.Empty;
}
Scenario 3: Representing the "Null" Character
If you need a char variable to represent "nothing" or the default state (similar to 0 for an integer), you cannot use ''. You must use the escape sequence for the Null Character (ASCII 0).
Example of error
public char GetSeparator()
{
// ⛔️ Error CS1011: Trying to return "nothing" via ''
return '';
}
Solution: Use \0 or default
The null character is written as '\0'. This is the default value of the char type.
public char GetSeparator()
{
// ✅ Correct: The Null Character (Unicode U+0000)
return '\0';
// ✅ Correct: More readable alternative
return default(char);
}
Conclusion
CS1011 enforces the rule that a character must exist.
- Check Quotes: Are you using single quotes
'? - Check Content: Is there a character inside?
- If you want an Empty value, switch to
stringand use"". - If you want a Space, type
' '. - If you want a Default value, type
'\0'.
- If you want an Empty value, switch to