Skip to main content

How to Resolve Error "CS0857: Indexed property must have all arguments optional" in C#

The Compiler Error CS0857 is a syntax restriction error related to Object Initializers and Indexed Properties. The message reads: "Indexed property 'Name' must have all arguments optional".

In C#, standard properties (obj.Prop) are accessed without arguments. However, in COM Interop (like Excel automation) or VB.NET libraries, you often encounter Named Indexed Properties (e.g., obj.Range["A1"]).

This error occurs when you try to assign a value to an indexed property inside an Object Initializer block (new Type { Prop = Value }) without providing the required index arguments. The compiler only allows this syntax if the property takes no arguments or if all its arguments are optional.

Understanding Indexed Properties in Initializers​

When you write:

var obj = new MyClass { MyProperty = 10 };

The compiler looks for a property setter MyProperty = value.

If MyProperty is actually an Indexed Property defined as MyProperty[int index] = value, the simple assignment syntax above is ambiguous (which index are you setting?). Unless the definition of MyProperty allows omitting the index (optional arguments), the compiler raises CS0857.

Scenario: Initializing COM/Indexed Properties​

This error is most common when working with libraries like Microsoft Office Interop. You might try to set a range or a cell value directly during the object creation phase.

Example of error​

You attempt to set Cells (which requires Row and Column) as if it were a simple property.

// Pseudo-code representing a COM Interface (e.g., Excel Worksheet)
public interface IWorksheet
{
// This property requires arguments [row, col]
string Cells[int row, int col] { get; set; }
}

public class ExcelAutomation
{
public void CreateSheet()
{
// ⛔️ Error CS0857: Indexed property 'Cells' must have all arguments optional.
// The compiler asks: "Which Cell are you trying to set to 'Header'?"
// You cannot supply arguments inside this specific initializer syntax.
var sheet = new ExcelSheet
{
Cells = "Header"
};
}
}

Solution: Assign After Initialization​

Since C# Object Initializers do not support passing arguments to specific named properties (unlike default indexers [0] = 1), you must perform this assignment after the object is constructed.

public class ExcelAutomation
{
public void CreateSheet()
{
// 1. Create the object first
var sheet = new ExcelSheet();

// 2. Assign the indexed property explicitly
// ✅ Correct: We provide the required arguments [1, 1].
sheet.Cells[1, 1] = "Header";
}
}
note

Default Indexers vs. Named Indexed Properties: C# does allow initializing the default indexer of a class: var list = new List<int> { [0] = 99 }; However, CS0857 applies specifically to Named properties (like Cells, Range, Item) that require arguments.

Conclusion​

CS0857 is the compiler enforcing that mandatory arguments cannot be skipped.

  1. Identify the Property: Is it a COM property like Range, Cells, or Item?
  2. Check Arguments: Does it require an index (e.g., [x, y])?
  3. Move the Logic: Remove the assignment from the { ... } initializer block and write it as a standalone statement: obj.Prop[index] = value;.