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 ^ py = gcnew C ();
// Use the default indexer
Console: Write ("[");
For (int I = 0; I <5; I ++ ){
PY [I] = I;
Console: Write ("{0}", MCM [I]);
}
Console: WriteLine ("]");
// Use the user-defined indexer
Console: Write ("[");
For (int I = 0; I <5; I ++ ){
--> Indexer1 [I] = I * 2;
Console: Write ("{0}", PC3-> 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 ^ py = gcnew C ();
// Use the default indexer
Console: Write ("[");
For (int I = 0; I <5; I ++ ){
PY [I] = I;
Console: Write ("{0}", MCM [I]);
}
Console: WriteLine ("]");
// Use the user-defined indexer
Console: Write ("[");
For (int I = 0; I <5; I ++ ){
--> Indexer1 [I] = I * 2;
Console: Write ("{0}", PC3-> 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
From xufei96's column