最終更新:2008-01-07 (月) 20:36:23 (5952d)  

インデクサ
Top / インデクサ

static void Main(string[] args)
{
  string s = "Hello!";
  for( int i=0; i<s.Length; i++ )
  {
    Console.WriteLine( s[i] );
  }
}
  • sは、string(System.Stringクラスの別名)型であるので、配列ではない。インデクサの機能が、文字列型のオブジェクトを配列であるかのように見せかけている
  • インデクサの実装
public char this[int index]
{
  get
  {
    return a[index];
  }
  set
  {
    a[index] = value;
  }
}