C# error: " An object reference is required for the non-static field, method, or property"

 Today, we're going to fix a C# Error: "An object reference is required for the non-static field, method, or property" that usually happens when we try to use a non-static element in a static scope. In the next few sections, we'll discuss what is "static" modifier keyword in C# is, why this problem happens, and how to resolve it.

What is "static" modifier keyword #

It's very important to understand what is static modifier keyword in C# before exploring the reason behind "An object reference is required for the non-static field, method, or property". A static modifier keyword can be applied to various elements such as properties, methods, class, and constructor. Here, we'll cover the static properties and static methods since these two are the main reason of "An object reference is required for the non-static field, method, or property".

Static properties are basically the properties of a class with the "static" keyword:


 public class Program
    {
        public static string txt = "Hello World";
    }
  

Here, we initialise a static property, txt under Program class.

Static methods are basically the methods with a "static" keyword. Here's the syntax of defining a static method:

<Access_Modifier> static <return_type> <method_name>([<params>])

Let's look at an example:


public static int multiply(int x, int y)
    {
        return x * y;
    }
  

So, what's the difference between the static method and instance method? A static method can be called without creating an Object of the class containing it. It can be invoked by the class itself or the Objects of the class. Conversely, an instance method is a method without static modifier keyword, which requires creating an Object of the class containing it so that the Object can be used to invoke that function.

Now, let'see how the error, "An object reference is required for the non-static field, method, or property" will happen.

Why "An object reference is required for the non-static field, method, or property" happens #

This error always happen when we're trying to use the non-static properties or non-static methods in a static scope. Let's look at them one by one:


 public class Program
    {
        public string txt = "Hello World";
        
        public static void Main(string[] args) 
        {
            Console.WriteLine(txt);
        }
    }
  

This is the output:


CS0120: An object reference is required for the non-static field, method, or property 'Program.txt'
  

This is because the static methodpublic static void Main(string[] args) is associated with the class itself, not the Object of the class containing it. Hence, the non-static propertytxt which requires an Object of the class to work will not work in a static method. Let's look at another example:


  public class Program
    {
        public static void Main(string[] args) 
        {
            int x = 3;
            int y = 2;
            Console.WriteLine(multiply(x, y));
        }

    public int multiply(int x, int y)
    {
        return x * y;
    }
}
  

This is the output:


CS0120: An object reference is required for the non-static field, method, or property 'Program.multiply(int,int)'
  

Same idea, the instance methodpublic int multiply(int x, int y) needs an Object of class, Program to be created to invoke it in a static method, which doesn't implicitly create an Object of the class, Program. Now, we've understood the causes of this error, let'see how to resolve it.

Solution #

We just need to be aware of the following rules to avoid this error:

  1. Avoid using non-static property in a static method.
  2. Avoid using non-static method in a static method.
  3. Explicitly initialise an Object of the class containing the non-static property before using it in a static method.
  4. Explicitly initialise an Object of the class containing the non-static method before using it in a static method.

Let's see some examples:


 public class Program
    {
        public static string txt = "Hello World";
        
        public static void Main(string[] args) 
        {
            Console.WriteLine(txt);
        }
    }
  

This is the output:


Hello World
  

This is because we're now having the static property, txt, and we can use it directly in the static method. Let's see another example:


 public class Program
    {
        public string txt = "Hello World";

        public static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine(p.txt);
        }
    }
  

This is the output:


Hello World
  

Here, we explicitly create an Objectp of the class, Program. Hence, we can utilise that object to use the non-static variable, txt in the static method.

Now, let's see the examples for "methods":


  public class Program
    {
        public static void Main(string[] args)
        {
            int x = 3;
            int y = 2;
            Console.WriteLine(multiply(x, y));
        }

        public static int multiply(int x, int y)
        {
            return x * y;
        }
    }
  

This is the output:


6
  

This is because we're now defining the method, public static int multiply(int x, int y) as static method, and we can use it directly in other static method. Let's see another example:


  public class Program
    {
        public static void Main(string[] args)
        {
            Program p = new Program();
            int x = 3;
            int y = 2;
            Console.WriteLine(p.multiply(x, y));
        }

        public  int multiply(int x, int y)
        {
            return x * y;
        }
    }
  

This is the output:


6
  

Here, we also explicitly create an Objectp of the class, Program. Hence, we can utilise that object to use the instance method, public int multiply(int x, int y) in the static method.

The Conclusion #

That's. Clear and simple understanding on this topic! In this study blog, we've learn what is a static modifier keyword, why "An object reference is required for the non-static field, method, or property" happens and how to resolve it. You should always make sure that you're not using non-static item in static item. If you want to do so, make sure you explicitly create an Object of the class containing the non-static items, so that you can utilise that Object to accessthem.


Next Post Previous Post
No Comment
Add Comment
comment url