error CS0120: An object reference is required for the non-static field, method, or property
means a static method, usually Main, is using a field or method
that belongs to an object, without an object. Create an instance with new and
call the member on it, or make the member static as well.
Which one is right depends on the data: if the value should live in one object (a score board, a cart, a player), create the object; if it is a plain helper with no state, make it static.
The error
...\Program.cs(7,9): error CS0120: An object reference is required for the non-static field, method, or property 'Program.AddScore(int)' [...\FixLab.csproj]
...\Program.cs(8,9): error CS0120: An object reference is required for the non-static field, method, or property 'Program.AddScore(int)' [...\FixLab.csproj]
...\Program.cs(9,37): error CS0120: An object reference is required for the non-static field, method, or property 'Program.total' [...\FixLab.csproj]
Build FAILED.
The project folder was shortened to ...; nothing else was changed.
Why it happens
This is the shape that produces it in almost every beginner program: a field and a helper
method written next to Main, in the same class.
class Program
{
int total;
static void Main()
{
AddScore(40);
AddScore(25);
Console.WriteLine($"Total: {total}");
}
void AddScore(int points)
{
total += points;
}
}
Main is static: it belongs to the class Program
itself and runs before any Program object exists. total and
AddScore are instance members: each Program object would have
its own total. Inside Main there is no object, so the compiler
cannot know whose total you mean, and it reports every use, three errors
here. The message names the member each time ('Program.AddScore(int)',
'Program.total'), which tells you exactly what is missing its object.
The fix
class Program
{
int total;
static void Main()
{
var board = new Program();
board.AddScore(40);
board.AddScore(25);
Console.WriteLine($"Total: {board.total}");
}
void AddScore(int points)
{
total += points;
}
}
new Program() creates the object, and every member is reached through it.
This version built with warnings as errors and printed Total: 65. In a real
program you would move total and AddScore into their own class,
say ScoreBoard, and create that instead; the idea is the same.
The other fix is to mark both total and AddScore as
static. It compiles, but then there is only one total for the whole program,
shared by everything. That suits a counter or a pure helper method, and it is the wrong
habit for data that belongs to something.
How it was reproduced
A dotnet new console project on .NET SDK 10.0.403 (net10.0, nullable and
implicit usings on, no packages), with the template's top-level statements replaced by the
classic class Program above. dotnet build failed with the three
CS0120 errors; the fixed version passed
dotnet build --no-incremental -p:TreatWarningsAsErrors=true and ran.
Frequently asked
- How do I call a non-static method from static Main in C#?
- Create an instance of the class and call the method on it: var app = new Program(); app.DoWork(); Alternatively make the method static, if it does not use any instance fields.
- Should I just make everything static to fix CS0120?
- Only for members that have no per-object state, such as pure helper methods. Static fields are shared by the whole program, which causes bugs once you need two of something or write tests.
- Why can a static method not use instance fields?
- A static method runs on the class, not on an object, so there is no this. Instance fields exist once per object, and without an object the compiler cannot tell which copy you mean.
More decoded compiler and runtime errors are in the Fixes category.