Skip to main content

How to Resolve Error "CS0620: Indexers cannot have void type" in C#

The Compiler Error CS0620 is a syntax definition error regarding Indexers. The message reads: "Indexers cannot have void type".

In C#, an Indexer allows an object to be indexed essentially like an array (e.g., myObject[0]). Because an indexer mimics accessing a variable or an array element, it must have a return type. It conceptually represents a piece of data. Declaring an indexer as void implies that accessing myObject[i] returns nothing, which makes the syntax var x = myObject[i] impossible and invalid.

This guide explains the correct syntax for indexers and how to choose the appropriate type.

Understanding Indexer Types​

An indexer is defined using the this keyword. Its signature looks like this: public ReturnType this[IndexType index] { get; set; }

  • ReturnType: Defines what type of data is returned by the get accessor, and what type of data is expected by the set accessor (value).
  • void: Is used for methods that perform an action but return no data.

Since an indexer is treated as a value access (L-value or R-value), it cannot be void. Even a write-only indexer (only set) needs a type to know what value is being written.

Scenario 1: Defining a Void Indexer​

This error occurs when a developer tries to create an indexer that perhaps triggers an action but doesn't return a value, or simply mistypes the declaration.

Example of error:

public class DataGrid
{
private string[] _cells = new string[10];

// ⛔️ Error CS0620: Indexers cannot have void type.
// The syntax implies 'something[i]' results in 'void'.
public void this[int index]
{
set
{
_cells[index] = "Updated";
}
// Even if there is no getter, 'void' is invalid because
// the setter needs to know what type 'value' is.
}
}

Solution 1: Specify the Data Type​

You must change void to the specific type of data the indexer is managing (e.g., string, int, Item).

Solution: if the indexer manages strings, declare it as string.

public class DataGrid
{
private string[] _cells = new string[10];

// ✅ Correct: The indexer is of type 'string'.
// The 'set' accessor now knows that the keyword 'value' is a string.
public string this[int index]
{
get { return _cells[index]; }
set { _cells[index] = value; }
}
}

class Program
{
static void Main()
{
var grid = new DataGrid();
grid[0] = "Hello"; // Valid
string val = grid[0]; // Valid
}
}
note

Even if you define a Write-Only indexer (no get), you still cannot use void. You must specify the type of the value being assigned: public string this[int i] { set { ... } }.

Solution 2: Use a Method for Actions​

If your intention was truly to perform an action (returning nothing) based on an index, and not to get or set data, then an indexer is the wrong tool. Use a standard Method instead.

Solution: replace this[...] with a named method like UpdateItem or ProcessItem.

public class DataGrid
{
private string[] _cells = new string[10];

// ✅ Correct: Methods can be void.
public void UpdateCell(int index)
{
_cells[index] = "Processed";
System.Console.WriteLine($"Cell {index} updated.");
}
}

Conclusion​

CS0620 enforces the concept that indexers represent data access, not behavior execution.

  1. Check the Declaration: Look at public type this[...].
  2. Define the Type: Change void to the actual data type stored or accessed (e.g., int, string, object).
  3. Switch to Methods: If you don't need to pass or return data, use a void Method(int index) instead of an indexer.