Compiler error ยท error CS1031
Type expected
A grammar slot only accepts a type name โ int, string, List<int>, Dog. It got a value instead. This is the type-shaped sibling of CS1001.
See it happen
var item = new 5(); // 'new' needs a type, and 5 is a value
Where types are required
| Slot | Broken | Fixed |
|---|---|---|
after new | new 5() | new Dog() |
| declaration | 5 count = 0; | int count = 0; |
| generic argument | new List<>() | new List<int>() |
| cast | (int) x โณ vs ()x | (int)x |
| parameter | void F(5) | void F(int n) |
Cause 1: a value where a type goes
var price = new 9.99(); // you probably meant a class
double price = 9.99; // you had the value already โ no new needed
Console.WriteLine(price);
This is the most common version: `new` is for creating objects. A double is a value you just write.
Cause 2: an empty generic argument list
var items = new List<>();
var items = new List<string>();
Console.WriteLine(items.Count);
Cause 3: a parameter with no type
void Show(5) // a parameter needs a type AND a name โ this has a value
{
}
void Show(int value) // type, then name
{
Console.WriteLine(value);
}
Show(0);
Cause 4: a typo made the type disappear
If you meant a type but the error says "type expected", check whether the name morphed into punctuation โ an accidental . or a deleted word. Then check separately whether the type exists: that second problem is CS0246.
| Error | Meaning |
|---|---|
| CS1031 | There is no type token in the slot at all |
| CS0246 | A type name is there, but unknown (typo / missing using) |
Frequently asked
What does CS1031 mean?
The compiler expected a type name and found a value or nothing. Put an actual type in the flagged slot โ after new, in a declaration, or inside a generic's angle brackets.
CS1031 vs CS0246?
CS1031: wrong shape โ no type where one is required. CS0246: right shape but unknown type. Fix the shape first, then the spelling.
Checklist
- Look at the slot the error points to:
new, declaration, generic, or cast. - Is there a value where a type belongs? Replace it with the type.
- Angle brackets empty? Fill in the type argument.
- Declaration with no type and no
var? Add one. - Type name looks right but still fails? That's CS0246 โ check spelling and usings.