Skip to main content

How to Resolve Error "CS0120: An object reference is required for the non-static field, method, or property" in C#

The Compiler Error CS0120 is one of the most common errors encountered by beginners in Object-Oriented Programming. The message reads: "An object reference is required for the non-static field, method, or property 'MemberName'".

This error occurs when you attempt to access an Instance member (a method, property, or variable that belongs to a specific object) from a Static context (like the static void Main method) without specifying which object instance you are referring to.

This guide explains the difference between static and instance contexts and provides three ways to resolve the error.

Understanding Static vs. Non-Static

To understand CS0120, think of a Blueprint (Class) and a House (Instance/Object).

  • Static (The Blueprint): Information that applies to the design itself (e.g., "All houses of this type have 2 doors"). This exists even if no houses are built.
  • Non-Static (The House): Information specific to one built house (e.g., " The kitchen light in this house is on").

You cannot ask the Blueprint: "Is the kitchen light on?" The Blueprint doesn't have a kitchen light; only specific built houses do.

CS0120 happens when you ask the Blueprint (Static Method) to access the Kitchen Light (Instance Property) without pointing to a specific House.

Scenario: Calling Instance Methods from Main

The Main method in a Console Application is static. This means it runs immediately when the program starts, before any objects are created. If you try to call a standard method from Main, the compiler blocks it.

Example of error:

public class Program
{
// This is an INSTANCE method (Non-static).
// It belongs to a specific object of type 'Program'.
public void SayHello()
{
System.Console.WriteLine("Hello!");
}

// This is a STATIC method. It runs without an object.
static void Main(string[] args)
{
// ⛔️ Error CS0120: An object reference is required...
// The compiler asks: "SayHello belongs to a Program object.
// Which Program object are you talking about?"
SayHello();
}
}

Solution 1: Make the Member Static

If the method or field does not rely on any specific object data (it doesn't use this, and it doesn't access other instance variables), you should mark it as static.

This moves the method from the "House" to the "Blueprint," allowing Main to call it directly.

public class Program
{
// ✅ Correct: Added 'static' keyword.
// Now this method belongs to the class, not an object.
public static void SayHello()
{
System.Console.WriteLine("Hello!");
}

static void Main(string[] args)
{
// ✅ Correct: Static methods can call other static methods directly.
SayHello();
}
}
note

When to use this: Use this when writing utility functions (like math calculations or simple loggers) that don't need to remember state between calls.

Solution 2: Create an Instance of the Class

If the method does need to store data or access state (variables) specific to an object, you cannot make it static. Instead, you must create an object (an instance) inside your static method and use that object to call the function.

public class Program
{
public string Message = "Hello from Object!";

public void SayHello()
{
// This method accesses 'this.Message', so it MUST be an instance method.
System.Console.WriteLine(this.Message);
}

static void Main(string[] args)
{
// 1. Create an instance (Build the House)
Program myProgram = new Program();

// 2. Call the method on that specific instance
// ✅ Correct: We provided the required object reference ('myProgram').
myProgram.SayHello();
}
}
note

When to use this: Use this for almost all "real" application logic. In standard OOP, Main is usually just a starting point that creates your main application object and runs it.

Solution 3: Passing the Reference

Sometimes you are in a static method, but you want to modify an object that was passed to you.

public class User
{
public string Name;
}

public class Program
{
// Static method acting on an object passed as a parameter
public static void RenameUser(User u)
{
// ✅ Correct: We are accessing the non-static field 'Name',
// but we are doing it via the object reference 'u'.
u.Name = "Alice";
}
}

Conclusion

CS0120 is the compiler enforcing the rules of Object-Oriented Programming.

  1. Check the keyword: Are you calling a method without static from a method with static?
  2. Option A (Utility): If the method is a generic tool, add static to its definition.
  3. Option B (Object): If the method represents an action on an entity, keep it as is, but create a new instance of the class before calling it.