BaseCollection.Item Property
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
Gets or sets the element in the collection at the specified position.
Namespace: Microsoft.SpeechServer.Dialog
Assembly: Microsoft.SpeechServer (in microsoft.speechserver.dll)
Syntax
'Declaration
Public Default Property Item ( _
index As Integer _
) As T
public T this [
int index
] { get; set; }
Parameters
- index
The zero-based index of the element to get or set.
Property Value
The element in the collection at the specified position.
Exceptions
Exception type | Condition |
---|---|
ArgumentNullException | An attempt is made to set the element to null. |
InvalidOperationException | An attempt is made to set one element of the collection to a value held by another element in the same collection. |
Remarks
This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[index]
.
Example
The following example is presented on the reference pages for many members of this class. The example instantiates a simple class object that inherits from BaseCollection and exercises many of the BaseCollection methods and properties. In this example, the statement coll_1[2] = "555-0100";
sets the item at index 2 in the collection to the specified string value. As noted in the code comments, any element at index 2 in the collection is overwritten. The statement string ph_number = coll_1[2];
reads the item at index 2 in the collection and stores it in ph_number.
// A simple class that inherits from the abstract class, BaseCollection<T>
public class bc : BaseCollection<string>
{
public bc() : base() { }
}
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
bc coll_1 = new bc();
// Add four phone numbers to the collection
coll_1.Add("555-0000");
coll_1.Add("555-0001");
coll_1.Add("555-0010");
coll_1.Add("555-0011");
// Copy the numbers in coll_1 to another collection, coll_2
bc coll_2 = new bc();
coll_2.AddRange(coll_1);
// Clear the coll_2 collection
coll_2.Clear();
// Check to see if coll_1 contains two phone numbers
bool isIn = coll_1.Contains("555-0100"); //false
isIn = coll_1.Contains("555-0000"); //true
// Determine the index of a specific phone number
int idx = coll_1.IndexOf("555-0010");
// Insert a new phone number into the collection
coll_1.Insert(2, "555-0100");
// Copy the elements in the collection to an array
string[] list = new string[10];
coll_1.CopyTo(list, 0);
// Remove the number that we just added
// If result gets set to true, the removal succeeded
bool result;
result = coll_1.Remove("555-0100");
// Try to remove a number that isn't in the collection
// If result gets set to false, the removal did not succeed
result = coll_1.Remove("555-1111");
// Insert the number into the collection at index 2
coll_1.Insert(2, "555-0100");
// Remove the number again
coll_1.RemoveAt(2);
// Determine the number of elements in the collection
int count = coll_1.Count;
// Use the collection's Item property to write a number to the collection
// This will overwrite whatever item was at that position in the collection
coll_1[2] = "555-0100";
// Use the collection's Item property to read a number in the collection
string ph_number = coll_1[2];
}
Thread Safety
All public static (Shared in Visual Basic) members of this type are thread-safe. Instance members are not guaranteed to be thread-safe.
Platforms
Development Platforms
Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition
Target Platforms
Windows Server 2003
See Also
Reference
BaseCollection Generic Class
BaseCollection Members
Microsoft.SpeechServer.Dialog Namespace