How to Resolve Error "CS0571: 'function' : cannot explicitly call operator or accessor" in C#
The Compiler Error CS0571 is a syntax restriction error. The message reads: "'MethodName' : cannot explicitly call operator or accessor".
In C#, features like Properties, Indexers, and Operators are "syntactic sugar." Under the hood, the compiler translates them into methods with special names (e.g., a property Name becomes get_Name and set_Name; a + operator becomes op_Addition).
However, C# intends for you to use the high-level syntax (obj.Name, a + b). The language specification forbids you from calling the underlying "secret" methods directly in code, even though they physically exist in the compiled assembly.
This guide explains how to replace manual method calls with the correct C# syntax.
Understanding Underlying Accessors
When code is compiled to Intermediate Language (IL), friendly names are converted to specific method names:
| C# Construct | Compiled Method Name |
|---|---|
int Value { get; } | int get_Value() |
int Value { set; } | void set_Value(int v) |
operator +(a, b) | op_Addition(a, b) |
operator ==(a, b) | op_Equality(a, b) |
this[int i] (Indexer) | get_Item(int i) |
CS0571 occurs because you tried to call get_Value() directly. C# says: "I generated that method for my own use; you must use the property syntax obj.Value instead."
Scenario 1: Property Getters and Setters
This often happens if you are porting code from C++ or reflection-heavy code where you saw the method name get_Name and thought it was the correct way to access the data.
Example of error
public class User
{
public string Name { get; set; }
}
public class Program
{
static void Main()
{
var u = new User();
u.Name = "Alice";
// ⛔️ Error CS0571: 'User.get_Name()': cannot explicitly call operator or accessor
// The method 'get_Name' exists in the DLL, but C# forbids calling it.
string n = u.get_Name();
// ⛔️ Error CS0571
u.set_Name("Bob");
}
}
Solution: Use Property Syntax
Treat the property like a variable.
public class Program
{
static void Main()
{
var u = new User();
// ✅ Correct: The compiler translates this to 'set_Name("Bob")' automatically.
u.Name = "Bob";
// ✅ Correct: The compiler translates this to 'get_Name()' automatically.
string n = u.Name;
}
}
Scenario 2: Operator Overloads
If you are inspecting a class using IntelliSense or an object browser, you might see methods like op_Addition or op_Inequality. These are the backing methods for operator overloads (+, !=). Calling them like standard static methods is invalid in C#.
Example of error
public class Vector
{
public int X;
public static Vector operator +(Vector a, Vector b) => new Vector { X = a.X + b.X };
}
public class Program
{
static void Main()
{
Vector v1 = new Vector { X = 1 };
Vector v2 = new Vector { X = 2 };
// ⛔️ Error CS0571: cannot explicitly call operator or accessor
Vector v3 = Vector.op_Addition(v1, v2);
}
}
Solution: Use Operators
Use the standard symbol associated with the operator.
public class Program
{
static void Main()
{
Vector v1 = new Vector { X = 1 };
Vector v2 = new Vector { X = 2 };
// ✅ Correct: Use the '+' symbol
Vector v3 = v1 + v2;
}
}
Exceptions: Some languages (like Visual Basic .NET in older versions) allowed calling op_Addition directly. C# is stricter about this to enforce readability.
Scenario 3: Indexers
In C#, an Indexer is defined as this[int index]. In the IL metadata, this is usually compiled as a property named Item with methods get_Item and set_Item.
Example of error
Trying to call get_Item on a List or Array.
using System.Collections.Generic;
public class Program
{
static void Main()
{
List<string> names = new List<string> { "Alice", "Bob" };
// ⛔️ Error CS0571: cannot explicitly call operator or accessor
string first = names.get_Item(0);
}
}
Solution: Use Brackets
Use the square bracket syntax.
public class Program
{
static void Main()
{
List<string> names = new List<string> { "Alice", "Bob" };
// ✅ Correct: Standard indexer access
string first = names[0];
}
}
Conclusion
CS0571 tells you that you are peeking behind the compiler's curtain.
- Don't use
get_/set_: Use properties directly (obj.Prop). - Don't use
op_: Use symbols (+,-,==). - Don't use
get_Item: Use indexers (obj[i]).
The only time you interact with these special names is via Reflection (which accesses metadata at runtime). In static C# code, use the clean syntax provided by the language.