C# Collection Item

Description

gets or sets the element at the specified index.

Syntax


public T this[
  int index
] { get; set; }

Parameters

  • index - The zero-based index of the element to get or set.

Example


using System;/*from  w w  w  .  j  a  va 2s.  c o  m*/
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection<string> myData = new Collection<string>();

        myData.Add("A");
        myData.Add("B");
        myData.Add("C");
        myData.Add("D");

        Console.WriteLine("\nmyData[2]: {0}", myData[2]);

        Console.WriteLine("\nmyData[2] = \"F\"");
        myData[2] = "F";

        foreach( string item in myData )
        {
            Console.WriteLine(item);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.ObjectModel »




Collection