在C#中访问C ++ / CLI重载的[]运算符

时间:2011-08-10 03:40:54

标签: c# c++-cli

我有一个C ++ / CLI类:

public ref class Foobar
{
    public:
        // methods here etc..

        // operator overload
        double operator[](int index);
}

如果我尝试过,我如何从C#访问Foobar

Foobar foo = new Foobar();
int i = foo[1]; 

我得到CS0021: Cannot apply indexing with [] to an expression of type 'Foobar'

1 个答案:

答案 0 :(得分:6)

operator[]在C ++ / CLI(以及所有.NET语言)中得到特殊处理 - 而不是被定义为运算符,它被定义为名为default的属性,称为the default index property

public ref class Foobar
{
public:
    // methods here etc..

    property double default[int];
}