C# Error CS1742 – An array access may not have a named argument specifier

C# Error

CS1742 – An array access may not have a named argument specifier

Reason for the Error & Solution

An array access may not have a named argument specifier

Example

The following sample generates CS1742:

// CS1742.cs (0,0)
public class B
{
    static void Main()
    {
        int[] arr = { };
        int s = arr[arr: 1];
    }
}

An array may not be declared with a named argument. This code generates CS1742 because using the name arr to refer to an argument when accessing the array is not syntactically correct.

To correct this error

Remove the use of named arguments when accessing an array to correct this error:

    static void Main()
    {
        int[] arr = { };
        int s = arr[1];
    }