Skip to main content

How to Resolve Error "CS1012: Too many characters in character literal" in C#

The Compiler Error CS1012 is a syntax error regarding the char data type. The message reads: "Too many characters in character literal".

In C#, single quotes (') are used exclusively for character literals. A char can hold exactly one Unicode character (16-bit). If you place more than one character inside single quotes (e.g., 'AB'), the compiler rejects it because that data does not fit into a char. This error is most often caused by confusing single quotes (') with double quotes ("), the latter being used for strings.

This guide explains the difference between characters and strings and how to fix this common syntax mistake.

Understanding Char vs. String

It is crucial to use the correct delimiters for the data type you intend to create:

  • char: Uses Single Quotes ('a'). Stores exactly one character.
  • string: Uses Double Quotes ("Hello"). Stores zero or more characters.

If you type 'Hello', the compiler sees single quotes and expects a single char. When it finds 'H', 'e', 'l'... it raises CS1012 because a char cannot hold a sequence.

Scenario 1: Confusing Quotes (Strings vs. Chars)

This is the most frequent cause. You intend to assign a text string to a variable, but you accidentally use single quotes (common in languages like JavaScript, Python, or SQL where both are often interchangeable).

Example of error

public class User
{
// ⛔️ Error CS1012: Too many characters in character literal
// Single quotes are for single characters only.
public string Name = 'Alice';
}

Solution: Use Double Quotes

Change the single quotes to double quotes to create a string literal.

public class User
{
// ✅ Correct: Double quotes denote a string.
public string Name = "Alice";
}

Solution: Use a Single Character

If you genuinely intended to use a char, ensure there is only one letter inside.

public class User
{
// ✅ Correct: Storing a single initial
public char Initial = 'A';
}

Scenario 2: Incorrect Escape Sequences

Sometimes you might try to represent a special character using a backslash, but if you type the escape sequence incorrectly, the compiler might interpret it as two distinct characters.

Example of error

For example, trying to type a backslash literal without escaping it properly.

public void Process()
{
// ⛔️ Error CS1012: Too many characters.
// The compiler sees '\' (escape start) and ' ' (space).
// Since '\ ' is not a valid escape sequence, it might interpret this
// as the character '\' followed by the character ' '.
char c = '\ ';
}

Solution: Correct Escaping

Ensure your escape sequence represents a single resulting character.

public void Process()
{
// ✅ Correct: To store a backslash, escape it.
char backslash = '\\';

// ✅ Correct: To store a single quote, escape it.
char quote = '\'';

// ✅ Correct: Unicode escape sequence (counts as 1 char).
char omega = '\u03A9';
}
note

Even though \u03A9 looks like 6 characters, it compiles down to a single 16-bit Unicode character, so it is valid inside single quotes.

Conclusion

CS1012 is the compiler telling you that you are trying to stuff a word into a letter-sized box.

  1. Check the Quotes: Are you using ' when you should be using "?
  2. Check the Length: Does the content inside '...' have more than one character?
  3. Refactor: If you need multiple characters, switch the type to string and use double quotes.