C# インデクサ(Indexer)
インデクサ(Indexer)オブジェクトが配列のように添字を使ってアクセスできるようにします。
クラスにインデクサを定義すると、そのクラスの動作はあたかも仮想配列(virtual array)のようになります。配列アクセス演算子を使用して[ ]このクラスのメンバーにアクセスできます。
構文
1次元インデクサの構文は次のとおりです。
element-type this[int index]
{
// get アクセサ
get
{
// index で指定された値を返します
}
// set アクセサ
set
{
// index で指定された値を設定します
}
}
{
// get アクセサ
get
{
// index で指定された値を返します
}
// set アクセサ
set
{
// index で指定された値を設定します
}
}
インデクサ(Indexer)の用途
インデクサの動作の宣言は、ある程度プロパティ(property)に似ています。プロパティ(property)と同様に、あなたはgetとsetアクセサを使用してインデクサを定義できます。しかし、プロパティは特定のデータメンバーを返すか設定しますが、インデクサはオブジェクトインスタンスの特定の値を返すか設定します。言い換えれば、インスタンスデータをより小さな部分に分割し、各部分をインデックス化して、各部分を取得または設定します。
プロパティ(property)の定義には、プロパティ名の指定が含まれます。インデクサは定義時に名前を持ちませんが、thisthis キーワードを持ち、それはオブジェクトインスタンスを指します。
例
using System;
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames()
{
for (int i = 0; i < size; i++)
namelist[i] = "N. A.";
}
public string this[int index]
{
get
{
string tmp;
if( index >= 0 && index <= size-1 )
{
tmp = namelist[index];
}
else
{
tmp = "";
}
return ( tmp );
}
set
{
if( index >= 0 && index <= size-1 )
{
namelist[index] = value;
}
}
}
static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[0] = "Zara";
names[1] = "Riz";
names[2] = "Nuha";
names[3] = "Asif";
names[4] = "Davinder";
names[5] = "Sunil";
names[6] = "Rubic";
for ( int i = 0; i < IndexedNames.size; i++ )
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
}
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames()
{
for (int i = 0; i < size; i++)
namelist[i] = "N. A.";
}
public string this[int index]
{
get
{
string tmp;
if( index >= 0 && index <= size-1 )
{
tmp = namelist[index];
}
else
{
tmp = "";
}
return ( tmp );
}
set
{
if( index >= 0 && index <= size-1 )
{
namelist[index] = value;
}
}
}
static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[0] = "Zara";
names[1] = "Riz";
names[2] = "Nuha";
names[3] = "Asif";
names[4] = "Davinder";
names[5] = "Sunil";
names[6] = "Rubic";
for ( int i = 0; i < IndexedNames.size; i++ )
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
}
上記のコードがコンパイルされて実行されると、次の結果が生成されます。
Zara Riz Nuha Asif Davinder Sunil Rubic N. A. N. A. N. A.
インデクサ(Indexer)のオーバーロード
インデクサ(Indexer)はオーバーロードできます。インデクサの宣言には複数のパラメータを含めることもでき、各パラメータは異なる型にすることができます。インデクサを整数型にする必要はありません。C# では、インデクサを文字列型などの他の型にすることができます。
次の例は、インデクサのオーバーロードを示しています。
例
using System;
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames()
{
for (int i = 0; i < size; i++)
{
namelist[i] = "N. A.";
}
}
public string this[int index]
{
get
{
string tmp;
if( index >= 0 && index <= size-1 )
{
tmp = namelist[index];
}
else
{
tmp = "";
}
return ( tmp );
}
set
{
if( index >= 0 && index <= size-1 )
{
namelist[index] = value;
}
}
}
public int this[string name]
{
get
{
int index = 0;
while(index < size)
{
if (namelist[index] == name)
{
return index;
}
index++;
}
return index;
}
}
static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[0] = "Zara";
names[1] = "Riz";
names[2] = "Nuha";
names[3] = "Asif";
names[4] = "Davinder";
names[5] = "Sunil";
names[6] = "Rubic";
// int パラメータを持つ最初のインデクサを使用
for (int i = 0; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
// string パラメータを持つ2番目のインデクサを使用
Console.WriteLine(names["Nuha"]);
Console.ReadKey();
}
}
}
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames()
{
for (int i = 0; i < size; i++)
{
namelist[i] = "N. A.";
}
}
public string this[int index]
{
get
{
string tmp;
if( index >= 0 && index <= size-1 )
{
tmp = namelist[index];
}
else
{
tmp = "";
}
return ( tmp );
}
set
{
if( index >= 0 && index <= size-1 )
{
namelist[index] = value;
}
}
}
public int this[string name]
{
get
{
int index = 0;
while(index < size)
{
if (namelist[index] == name)
{
return index;
}
index++;
}
return index;
}
}
static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[0] = "Zara";
names[1] = "Riz";
names[2] = "Nuha";
names[3] = "Asif";
names[4] = "Davinder";
names[5] = "Sunil";
names[6] = "Rubic";
// int パラメータを持つ最初のインデクサを使用
for (int i = 0; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
// string パラメータを持つ2番目のインデクサを使用
Console.WriteLine(names["Nuha"]);
Console.ReadKey();
}
}
}
上記のコードがコンパイルされて実行されると、次の結果が生成されます。
Zara Riz Nuha Asif Davinder Sunil Rubic N. A. N. A. N. A. 2その他の拡張