C# Error CS0571 – ‘{0}’: cannot explicitly call operator or accessor

C# Error

CS0571 – ‘{0}’: cannot explicitly call operator or accessor

Reason for the Error & Solution

‘function’ : cannot explicitly call operator or accessor

Certain operators have internal names. For example, op_Increment is the internal name of the ++ operator. You should not use or explicitly call such method names.

The following sample generates CS0571:

// CS0571.cs  
public class MyClass  
{  
   public static MyClass operator ++ (MyClass c)  
   {  
      return null;  
   }  
  
   public static int prop  
   {  
      get  
      {  
         return 1;  
      }  
      set  
      {  
      }  
   }  
  
   public static void Main()  
   {  
      op_Increment(null);   // CS0571  
      // use the increment operator as follows  
      // MyClass x = new MyClass();  
      // x++;  
  
      set_prop(1);      // CS0571  
      // try the following line instead  
      // prop = 1;  
   }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...