Error: An object reference is required for the non-static field, method, or property

Table of Contents Static ClassFeatures of static class:Non-Static ClassError CS0120: An object reference is required for the non-static field, method, or property1. In C#, this error occurs when an object attempts to directly reference a …

an object reference is required for the non-static field

Static Class

The classes that cannot be instantiated and are sealed to inherit due to their static nature are called static classes. These classes are based on static members and methods. As static classes do not create instances, the static member is always retrieved by the class name.

We cannot use the “new” operator in static class to declare a class type variable. Suppose if a static class created with the named SampleClass  that contains a static method named MethodC with the public modifier, the method is called as shown in the following code:

SampleClass.MethoC();

The static class creates only with members that should be static and a private constructor. A private constructor helps in preventing the class from being instantiated. The main advantage of using Static class is that compiler can make sure no addition of instance member accidentally and guaranteed that no instance of that class will be created.

Inheritance of members is possible by Object, and static class does not create the instance. It means the static class has no object. Non-static classes should declare a static constructor if the non-static class contains static members that need non-trivial declaration—the static class created by using static keyword before the class name.

Features of static class:

  • Members must be static.
  • It cannot be instantiated.
  • Sealed to inherit.
  • No Instance constructor.

Non-Static Class

Non-static class is the same as a static class, but unlike a static class, it can inherit an instantiated variable using a new operator variable of class type. The constructor of a static field can easily declare inside a non-static classic

Error CS0120: An object reference is required for the non-static field, method, or property

An error occurred when we call a non-static field, property, or method from a static class. Static members are static and do not call a non-static field. Static classes are sealed as we cannot inherit them. The static method is used when no additional member wants to add accidentally.

A reference to the object is needed to access the non-static method. If you need to call a non-static field from a static class, construct an object. After creating an instance of an object, you can reference that object.

This error is expected in two situations. Expected Errors in the non-static field and their solutions are below:

1. In C#, this error occurs when an object attempts to directly reference a non-static variable in a static method.

public string name;
public static void Displayname()
{ 
 name = "Mud";
}

The above error solves to create an instance by using a new operator variable of class type, which allocates memory direct to the object.

public string name;
public static void Displayname()
{ 
    MyScript script = new MyScript();
    
    script.name = "Mud";
}

1. This error experienced when inside a static method; you want to call another non-static method.

public class MyNewCode
{
 public static void Main()  
    {  
        CallFun();   // Error CS0120
        // Use the following two lines instead:
        // var anInstanceof MyNewCode = new MyNewCode
();
        // anInstanceofMyClass.TestCall();
   }  public void CallFun ()
   {
   }}

The above error solves to create an instance by using a new operator variable of class type, which allocates memory direct to the object.

public class MyNewCode
{
 public static void Main()  
    {  CallFun();   
      var anInstanceof MyNewCode = new MyNewCode
();
      anInstanceofMyClass.TestCall();
   }  public void CallFun ()
   {  
   }}

Conclusion

In this article, we discussed a programming error that is commonly faced by new programmers. The reason behind the Error occurrence is that, calls a non-static field, property, or method from a static class. Static members are static in nature and not callable in the non-static field. Static members cannot be instantiated and inherited due to their static nature. 

Categories C#

Leave a Comment