我有一个C++/CLI类:
public ref class Foobar
{
public:
// methods here etc..
// operator overload
double operator[](int index);
}
Run Code Online (Sandbox Code Playgroud)
如果我Foobar尝试过,我如何从C#访问:
Foobar foo = new Foobar();
int i = foo[1];
Run Code Online (Sandbox Code Playgroud)
我明白了 CS0021: Cannot apply indexing with [] to an expression of type 'Foobar'
operator[]在C++/CLI(以及所有.NET语言)中得到特殊处理 - 而不是被定义为运算符,它被定义为一个名为的属性default,称为默认索引属性.
public ref class Foobar
{
public:
// methods here etc..
property double default[int];
}
Run Code Online (Sandbox Code Playgroud)