This documentation is archived and is not being maintained.

Compiler Error CS0021 

Error Message

Cannot apply indexing with [] to an expression of type 'type'

An attempt was made to access a value through an indexer on a data type that does not support Indexers (C# Programming Guide).

You may get CS0021 when trying to use an indexer in a C++ assembly. In this case, decorate the C++ class with the DefaultMember attribute so the C# compiler knows which indexer is the default. The following sample generates CS0021.


This file compiles to a .dll file—with the DefaultMember attribute commented out—in order to generate the error.

// CPP0021.cpp
// compile with: /clr /LD
using namespace System::Reflection;
// Uncomment the following line to resolve
public ref class MyClassMC
        property int myItem[int]
            int get(int i){  return 5; }
            void set(int i, int value) {}

The following is the C# file that calls the .dll file. This file attempts to access the class via an indexer, but because no member was declared as the default indexer to be used, the error is generated.

// CS0021.cs
// compile with: /reference:CPP0021.dll
public class MyClass
    public static void Main()
        MyClassMC myMC = new MyClassMC();
        int j = myMC[1]; // CS0021