Skip to content
Latchkey

C# "CS0120: an object reference is required for the non-static member" in CI

CS0120 means code in a static context (a static method, Main, or a static field initializer) tries to use an instance member without an instance. The member needs a this, but there is none available.

What this error means

The build fails with CS0120 naming the non-static member. It reproduces deterministically.

dotnet
Program.cs(12,9): error CS0120: An object reference is required for the non-static field,
method, or property 'Calculator.Add(int, int)'

Common causes

A static method calls an instance member

Main or another static method invokes an instance method/property without creating an instance first.

A member should have been static

A helper that takes no instance state was written as an instance member but is used from static code.

How to fix it

Provide an instance or make the member static

  1. Create an instance and call the member on it, or
  2. Mark the member static if it does not use instance state.
  3. Rebuild.
C#
var calc = new Calculator();
int sum = calc.Add(2, 3);

How to prevent it

  • Mark stateless helpers static so call sites are unambiguous.
  • Let analyzers suggest static for members that do not touch instance state.

Frequently asked questions

What causes ""CS0120: object reference required""?
Main or another static method invokes an instance method/property without creating an instance first.
How do I fix "CS0120: object reference required"?
Provide an instance or make the member static

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card