Compiler error ยท error CS1519
Invalid token 'X' in class, struct, or interface member declaration
Inside a class body the compiler accepts declarations โ fields, methods, properties, constructors. It found something else, so it doesn't know what member you were declaring.
See it happen
class Counter
{
5 // a value is not a member declaration
}
Cause 1: a statement straight in the class body
This is the big one for beginners โ executable code must live inside a method:
class Bad
{
int x = 0;
x = 5; // โ CS1519 โ statements belong in a method
}
class Good
{
int x = 0;
public void SetToFive()
{
x = 5; // โ the statement lives in a method
}
public int Read() { return x; }
}
var g = new Good();
g.SetToFive();
Console.WriteLine(g.Read());
Cause 2: a field without a type
class Config
{
maxRetries = 3; // missing the type โ the compiler reads this as a member name
}
class Config
{
public int maxRetries = 3; // type + name + initializer
}
Console.WriteLine(new Config().maxRetries);
Cause 3: a method written outside the class
If the closing brace of the class came too early, the method lands at file scope. Top-level methods are legal in modern C#, so this can be subtle โ but if the method references the class's fields, it fails:
class Counter
{
private int count = 0;
} // โ class closed here
public void Increment() // now outside the class
{
count++; // count no longer in scope
}
Cause 4: a stray character
A lone comma, semicolon in the wrong place, or a `#` from a copy-paste can trigger the same message. Read the token the compiler names โ it's telling you exactly which character it choked on.
Frequently asked
What does CS1519 mean?
An invalid token appeared where a member declaration was expected. Move statements into a method, and give every field a type.
Why can't I run code directly in a class?
A class body accepts declarations only. Executable statements belong in a method or constructor.
Checklist
- Read the token in the message โ that exact character is the problem.
- Is it a statement? Move it into a method.
- Is it a field name with no type? Add the type.
- Did a brace close too early, pushing a method out of the class?
- Copy-pasted from somewhere? Look for stray punctuation at the paste boundary.