次の方法で共有


CollectionBase.System.Collections.IList.Item プロパティ

メモ : この名前空間、クラス、およびメンバは、.NET Framework Version 1.1 だけでサポートされています。

指定したインデックスにある要素を取得または設定します。

[C#] C# では、このプロパティは CollectionBase クラスのインデクサになります。

Private Default Property Item( _
   ByVal index As Integer _) As Object Implements IList.Item
[C#]
object IList.this[
   intindex] {get; set;}
[C++]
private: __property Object* System::Collections::IList::get_Item(intindex);private: __property void System::Collections::IList::set_Item(intindex,   Object*);
[JScript]
private function get IList.get_Item(index : int) : Object;private function set IList.set_Item(index : int, value : Object);またはprivate function get IList.get_Item(index : int) : Object;private function set IList.set_Item(index : int, value : Object);

[JScript] JScript では、この型で定義されている既定のインデックス プロパティを使用することができます。しかし、独自のインデックス プロパティを明示的に定義することはできません。ただし、このクラスの expando 属性を指定すると、既定のインデックス プロパティが提供されます。提供されるインデックス プロパティの型は Object 型であり、インデックス型は String になります。

引数 [JScript]

  • index
    取得または設定する要素の、0 から始まるインデックス番号。

パラメータ [Visual Basic, C#, C++]

  • index
    取得または設定する要素の、0 から始まるインデックス番号。

プロパティ値

指定したインデックスにある要素。

実装

IList.Item

例外

例外の種類 条件
ArgumentOutOfRangeException index が 0 未満です。

または

index が Count 以上です。

解説

このプロパティでは、 myCollection[index] という構文を使用して、コレクション内の特定の要素にアクセスできます。

使用例

[Visual Basic, C#, C++] CollectionBase クラスを実装し、その実装を使用して Int16 オブジェクトのコレクションを作成する方法については、次のコード例を参照してください。

 
Imports System
Imports System.Collections


Public Class Int16Collection
   Inherits CollectionBase


   Default Public Property Item(index As Integer) As Int16
      Get
         Return CType(List(index), Int16)
      End Get
      Set
         List(index) = value
      End Set
   End Property


   Public Function Add(value As Int16) As Integer
      Return List.Add(value)
   End Function 'Add

   Public Function IndexOf(value As Int16) As Integer
      Return List.IndexOf(value)
   End Function 'IndexOf


   Public Sub Insert(index As Integer, value As Int16)
      List.Insert(index, value)
   End Sub 'Insert


   Public Sub Remove(value As Int16)
      List.Remove(value)
   End Sub 'Remove


   Public Function Contains(value As Int16) As Boolean
      ' If value is not of type Int16, this will return false.
      Return List.Contains(value)
   End Function 'Contains


   Protected Overrides Sub OnInsert(index As Integer, value As [Object])
      ' Insert additional code to be run only when inserting values.
   End Sub 'OnInsert


   Protected Overrides Sub OnRemove(index As Integer, value As [Object])
      ' Insert additional code to be run only when removing values.
   End Sub 'OnRemove


   Protected Overrides Sub OnSet(index As Integer, oldValue As [Object], newValue As [Object])
      ' Insert additional code to be run only when setting values.
   End Sub 'OnSet


   Protected Overrides Sub OnValidate(value As [Object])
      If Not value.GetType() Is Type.GetType("System.Int16") Then
         Throw New ArgumentException("value must be of type Int16.", "value")
      End If
   End Sub 'OnValidate 

End Class 'Int16Collection


Public Class SamplesCollectionBase

   Public Shared Sub Main()

      ' Creates and initializes a new CollectionBase.
      Dim myI16 As New Int16Collection()

      ' Adds elements to the collection.
      myI16.Add( 1 )
      myI16.Add( 2 )
      myI16.Add( 3 )
      myI16.Add( 5 )
      myI16.Add( 7 )

      ' Displays the contents of the collection using the enumerator.
      Console.WriteLine("Initial contents of the collection:")
      PrintIndexAndValues(myI16)
      
      ' Searches the collection with Contains and IndexOf.
      Console.WriteLine("Contains 3: {0}", myI16.Contains(3))
      Console.WriteLine("2 is at index {0}.", myI16.IndexOf(2))
      Console.WriteLine()
      
      ' Inserts an element into the collection at index 3.
      myI16.Insert(3, 13)
      Console.WriteLine("Contents of the collection after inserting at index 3:")
      PrintIndexAndValues(myI16)
      
      ' Gets and sets an element using the index.
      myI16(4) = 123
      Console.WriteLine("Contents of the collection after setting the element at index 4 to 123:")
      PrintIndexAndValues(myI16)
      
      ' Removes an element from the collection.
      myI16.Remove(2)

      ' Displays the contents of the collection using the index.
      Console.WriteLine("Contents of the collection after removing the element 2:")
      Dim i As Integer
      For i = 0 To myI16.Count - 1
         Console.WriteLine("   [{0}]:   {1}", i, myI16(i))
      Next i

   End Sub 'Main


   Public Shared Sub PrintIndexAndValues(myCol As Int16Collection)

      Dim i As Integer = 0
      Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
      While myEnumerator.MoveNext()
         Console.WriteLine("   [{0}]:   {1}", i, myEnumerator.Current)
         i += 1
      End While
      Console.WriteLine()

   End Sub 'PrintIndexAndValues

End Class 'SamplesCollectionBase


'This code produces the following output.
'
'Initial contents of the collection:
'   [0]:   1
'   [1]:   2
'   [2]:   3
'   [3]:   5
'   [4]:   7
'
'Contains 3: True
'2 is at index 1.
'
'Contents of the collection after inserting at index 3:
'   [0]:   1
'   [1]:   2
'   [2]:   3
'   [3]:   13
'   [4]:   5
'   [5]:   7
'
'Contents of the collection after setting the element at index 4 to 123:
'   [0]:   1
'   [1]:   2
'   [2]:   3
'   [3]:   13
'   [4]:   123
'   [5]:   7
'
'Contents of the collection after removing the element 2:
'   [0]:   1
'   [1]:   3
'   [2]:   13
'   [3]:   123
'   [4]:   7



[C#] 
using System;
using System.Collections;

public class Int16Collection : CollectionBase  {

   public Int16 this[ int index ]  {
      get  {
         return( (Int16) List[index] );
      }
      set  {
         List[index] = value;
      }
   }

   public int Add( Int16 value )  {
      return( List.Add( value ) );
   }

   public int IndexOf( Int16 value )  {
      return( List.IndexOf( value ) );
   }

   public void Insert( int index, Int16 value )  {
      List.Insert( index, value );
   }

   public void Remove( Int16 value )  {
      List.Remove( value );
   }

   public bool Contains( Int16 value )  {
      // If value is not of type Int16, this will return false.
      return( List.Contains( value ) );
   }

   protected override void OnInsert( int index, Object value )  {
      // Insert additional code to be run only when inserting values.
   }

   protected override void OnRemove( int index, Object value )  {
      // Insert additional code to be run only when removing values.
   }

   protected override void OnSet( int index, Object oldValue, Object newValue )  {
      // Insert additional code to be run only when setting values.
   }

   protected override void OnValidate( Object value )  {
      if ( value.GetType() != Type.GetType("System.Int16") )
         throw new ArgumentException( "value must be of type Int16.", "value" );
   }

}


public class SamplesCollectionBase  {

   public static void Main()  {
 
      // Creates and initializes a new CollectionBase.
      Int16Collection myI16 = new Int16Collection();

      // Adds elements to the collection.
      myI16.Add( (Int16) 1 );
      myI16.Add( (Int16) 2 );
      myI16.Add( (Int16) 3 );
      myI16.Add( (Int16) 5 );
      myI16.Add( (Int16) 7 );

      // Displays the contents of the collection using the enumerator.
      Console.WriteLine( "Initial contents of the collection:" );
      PrintIndexAndValues( myI16 );

      // Searches the collection with Contains and IndexOf.
      Console.WriteLine( "Contains 3: {0}", myI16.Contains( 3 ) );
      Console.WriteLine( "2 is at index {0}.", myI16.IndexOf( 2 ) );
      Console.WriteLine();

      // Inserts an element into the collection at index 3.
      myI16.Insert( 3, (Int16) 13 );
      Console.WriteLine( "Contents of the collection after inserting at index 3:" );
      PrintIndexAndValues( myI16 );

      // Gets and sets an element using the index.
      myI16[4] = 123;
      Console.WriteLine( "Contents of the collection after setting the element at index 4 to 123:" );
      PrintIndexAndValues( myI16 );

      // Removes an element from the collection.
      myI16.Remove( (Int16) 2 );

      // Displays the contents of the collection using the index.
      Console.WriteLine( "Contents of the collection after removing the element 2:" );
      for ( int i = 0; i < myI16.Count; i++ )  {
         Console.WriteLine( "   [{0}]:   {1}", i, myI16[i] );
      }

   }
 
   public static void PrintIndexAndValues( Int16Collection myCol )  {
      int i = 0;
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   [{0}]:   {1}", i++, myEnumerator.Current );
      Console.WriteLine();
   }
}


/* 
This code produces the following output.

Initial contents of the collection:
   [0]:   1
   [1]:   2
   [2]:   3
   [3]:   5
   [4]:   7

Contains 3: True
2 is at index 1.

Contents of the collection after inserting at index 3:
   [0]:   1
   [1]:   2
   [2]:   3
   [3]:   13
   [4]:   5
   [5]:   7

Contents of the collection after setting the element at index 4 to 123:
   [0]:   1
   [1]:   2
   [2]:   3
   [3]:   13
   [4]:   123
   [5]:   7

Contents of the collection after removing the element 2:
   [0]:   1
   [1]:   3
   [2]:   13
   [3]:   123
   [4]:   7

*/


[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

public __gc class Int16Collection : public CollectionBase 
{
public:
   __property Int16 get_Item( int index )
   {
      return *__try_cast<__box Int16*>(List->Item[index]);
   }
   __property void set_Item( int index, Int16 value )
   {
      List->Item[index] = __box(value);
   }

   int Add(Int16 value)
   {
      return(List->Add(__box(value)));
   }

   int IndexOf(Int16 value) 
   {
      return(List->IndexOf(__box(value)));
   }

   void Insert(int index, Int16 value) 
   {
      List->Insert(index, __box(value));
   }

   void Remove(Int16 value)
   {
      List->Remove(__box(value));
   }

   bool Contains(Int16 value)
   {
      // If value is not of type Int16, this will return false.
      return(List->Contains(__box(value)));
   }

protected:
   void OnInsert(int index, Object* value) 
   {
      // Insert additional code to be run only when inserting values.
   }

   void OnRemove(int index, Object* value) 
   {
      // Insert additional code to be run only when removing values.
   }

   void OnSet(int index, Object* oldValue, Object* newValue) 
   {
      // Insert additional code to be run only when setting values.
   }

   void OnValidate(Object* value) 
   {
      if (value->GetType() != Type::GetType(S"System.Int16"))
         throw new ArgumentException(S"value must be of type Int16.", S"value");
   }
};

void PrintIndexAndValues(Int16Collection* myCol)
{
   int i = 0;
   System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator();
   while (myEnumerator->MoveNext())
      Console::WriteLine(S"   [ {0}]: {1}", __box(i++), myEnumerator->Current);
   Console::WriteLine();
}

int main() 
{

   // Creates and initializes a new CollectionBase.
   Int16Collection* myI16 = new Int16Collection();

   // Adds elements to the collection.
   myI16->Add((Int16) 1);
   myI16->Add((Int16) 2);
   myI16->Add((Int16) 3);
   myI16->Add((Int16) 5);
   myI16->Add((Int16) 7);

   // Displays the contents of the collection using the enumerator.
   Console::WriteLine(S"Initial contents of the collection:");
   PrintIndexAndValues(myI16);

   // Searches the collection with Contains and IndexOf.
   Console::WriteLine(S"Contains 3: {0}", __box(myI16->Contains(3)));
   Console::WriteLine(S"2 is at index {0}.", __box(myI16->IndexOf(2)));
   Console::WriteLine();

   // Inserts an element into the collection at index 3.
   myI16->Insert(3, (Int16) 13);
   Console::WriteLine(S"Contents of the collection after inserting at index 3:");
   PrintIndexAndValues(myI16);

   // Gets and sets an element using the index.
   myI16->Item[4] = 123;
   Console::WriteLine(S"Contents of the collection after setting the element at index 4 to 123:");
   PrintIndexAndValues(myI16);

   // Removes an element from the collection.
   myI16->Remove((Int16) 2);

   // Displays the contents of the collection using the index.
   Console::WriteLine(S"Contents of the collection after removing the element 2:");
   for (int i = 0; i < myI16->Count; i++)
   {
      Console::WriteLine(S"   [ {0}]: {1}", __box(i), __box(myI16->Item[i]));
   }

}

/* 
This code produces the following output.

Initial contents of the collection:
[0]:   1
[1]:   2
[2]:   3
[3]:   5
[4]:   7

Contains 3: True
2 is at index 1.

Contents of the collection after inserting at index 3:
[0]:   1
[1]:   2
[2]:   3
[3]:   13
[4]:   5
[5]:   7

Contents of the collection after setting the element at index 4 to 123:
[0]:   1
[1]:   2
[2]:   3
[3]:   13
[4]:   123
[5]:   7

Contents of the collection after removing the element 2:
[0]:   1
[1]:   3
[2]:   13
[3]:   123
[4]:   7

*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

CollectionBase クラス | CollectionBase メンバ | System.Collections 名前空間 | Count