How to: Use Indexed Properties
An indexed property typically exposes a data structure that is accessed using a subscript operator.
A default indexed property allows the user to access the data structure via the class name, whereas a user-defined indexed property requires the user to specify the property name to access the data structure.
For information on accessing a default indexer via the this pointer, see Semantics of the this Pointer in Value and Reference Types.
For information on consuming an indexer authored in C#, see How to: Consume a C# Indexer.
Example
The following code sample shows how to use default and user defined indexed properties.
[cpp] // mcppv2_property_2.cpp
// compile with: /clr
using namespace System;
public ref class C {
array<int>^ MyArr;
public:
C() {
MyArr = gcnew array<int>(5);
}
// default indexer
property int default[int] {
int get(int index) {
return MyArr[index];
}
void set(int index, int value) {
MyArr[index] = value;
}
}
// user-defined indexer
property int indexer1[int] {
int get(int index) {
return MyArr[index];
}
void set(int index, int value) {
MyArr[index] = value;
}
}
};
int main() {
C ^ MyC = gcnew C();
// use the default indexer
Console::Write("[ ");
for (int i = 0 ; i < 5 ; i++) {
MyC[i] = i;
Console::Write("{0} ", MyC[i]);
}
Console::WriteLine("]");
// use the user-defined indexer
Console::Write("[ ");
for (int i = 0 ; i < 5 ; i++) {
MyC->indexer1[i] = i * 2;
Console::Write("{0} ", MyC->indexer1[i]);
}
Console::WriteLine("]");
}
// mcppv2_property_2.cpp
// compile with: /clr
using namespace System;
public ref class C {
array<int>^ MyArr;
public:
C() {
MyArr = gcnew array<int>(5);
}
// default indexer
property int default[int] {
int get(int index) {
return MyArr[index];
}
void set(int index, int value) {
MyArr[index] = value;
}
}
// user-defined indexer
property int indexer1[int] {
int get(int index) {
return MyArr[index];
}
void set(int index, int value) {
MyArr[index] = value;
}
}
};
int main() {
C ^ MyC = gcnew C();
// use the default indexer
Console::Write("[ ");
for (int i = 0 ; i < 5 ; i++) {
MyC[i] = i;
Console::Write("{0} ", MyC[i]);
}
Console::WriteLine("]");
// use the user-defined indexer
Console::Write("[ ");
for (int i = 0 ; i < 5 ; i++) {
MyC->indexer1[i] = i * 2;
Console::Write("{0} ", MyC->indexer1[i]);
}
Console::WriteLine("]");
}
Output
[ 0 1 2 3 4 ] [ 0 2 4 6 8 ]
Example
This sample shows how to call the default indexer through the this pointer.
[cpp] // call_default_indexer_through_this_pointer.cpp
// compile with: /clr /c
value class Position {
public:
Position(int x, int y) : position(gcnew array<int, 2>(100, 100)) {
this->default[x, y] = 1;
}
property int default[int, int] {
int get(int x, int y) {
return position[x, y];
}
void set(int x, int y, int value) {}
}
private:
array<int, 2> ^ position;
};
// call_default_indexer_through_this_pointer.cpp
// compile with: /clr /c
value class Position {
public:
Position(int x, int y) : position(gcnew array<int, 2>(100, 100)) {
this->default[x, y] = 1;
}
property int default[int, int] {
int get(int x, int y) {
return position[x, y];
}
void set(int x, int y, int value) {}
}
private:
array<int, 2> ^ position;
};
Example
This sample shows how you can use DefaultMemberAttribute to specify the default indexer.
[cpp] // specify_default_indexer.cpp
// compile with: /LD /clr
using namespace System;
[Reflection::DefaultMember("XXX")]
public ref struct Squares {
property Double XXX[Double] {
Double get(Double data) {
return data*data;
}
}
};
// specify_default_indexer.cpp
// compile with: /LD /clr
using namespace System;
[Reflection::DefaultMember("XXX")]
public ref struct Squares {
property Double XXX[Double] {
Double get(Double data) {
return data*data;
}
}
};
Example
This sample consumes the metadata created in the previous example.
[cpp] // consume_default_indexer.cpp
// compile with: /clr
#using "specify_default_indexer.dll"
int main() {
Squares ^ square = gcnew Squares();
System::Console::WriteLine("{0}", square[3]);
}
// consume_default_indexer.cpp
// compile with: /clr
#using "specify_default_indexer.dll"
int main() {
Squares ^ square = gcnew Squares();
System::Console::WriteLine("{0}", square[3]);
}
Output
摘自 xufei96的專欄