Compiler error · error CS1001
Identifier expected
The grammar has a slot that only accepts a name, and it got something else. The message does not always say which name — so the skill is recognising the slots.
See it happen
int 5 = x; // a variable name cannot be a number
The name slots you'll meet
| Construct | Needs a name | Broken | Fixed |
|---|---|---|---|
| variable declaration | variable | int = 5; | int x = 5; |
| class declaration | class | class { } | class Foo { } |
| method declaration | method | void () { } | void Run() { } |
| parameter | parameter | void F(int) { } | void F(int n) { } |
new expression | type | var d = new (); | var d = new Dog(); |
Cause 1: the name was deleted
string = "hello"; // which variable should hold this?
string greeting = "hello";
Console.WriteLine(greeting);
Cause 2: a keyword used as a name
int class = 5; // 'class' already means something to the compiler
Two fixes — rename (better), or escape it with @ (legal but ugly):
int @class = 5; // the @ says "treat this as a plain name"
Console.WriteLine(@class);
Cause 3: a missing parameter name
void Greet(string)
{
Console.WriteLine("hi");
}
void Greet(string name)
{
Console.WriteLine("hi, " + name);
}
Greet("Ada");
Cause 4: a generic with an empty type argument
var list = new List<>(); // which kind of List?
var list = new List<int>();
Console.WriteLine(list.Count);
Frequently asked
What does CS1001 mean?
The compiler expected a name and found a number, operator or misplaced keyword. Supply a valid identifier at the flagged position.
Can I use a keyword as a name?
With @, yes (@class) — but renaming is almost always better.
Checklist
- Locate the slot: declaration, class, method, parameter, or
new. - Is a name missing entirely? Add one.
- Is it a keyword? Rename, or escape with
@. - Generic type missing?
new List<int>()notnew List<>().