我有一个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'
答案 0 :(得分:6)
operator[]
在C ++ / CLI(以及所有.NET语言)中得到特殊处理 - 而不是被定义为运算符,它被定义为名为default
的属性,称为the default index property
public ref class Foobar
{
public:
// methods here etc..
property double default[int];
}