ManagementObjectCollection does not implements Indexers, but yes you can you FirstOrDefault extension function if you are using linq but geeks who are using .net 3 or earlier (like me still working on 1.1) can use following code, it is standard way of getting first item from any collection implemented IEnumerable interface.
//TODO: Do the Null and Count Check before following lines
IEnumerator enumerator = collection.GetEnumerator();
enumerator.MoveNext();
ManagementObject mo = (ManagementObject)enumerator.Current;
following are two different ways to retrieve ManagementObject from any index
private ManagementObject GetItem(ManagementObjectCollection collection, int index)
{
//TODO: do null handling
IEnumerator enumerator = collection.GetEnumerator();
int currentIndex = 0;
while (enumerator.MoveNext())
{
if (currentIndex == index)
{
return enumerator.Current as ManagementObject;
}
currentIndex++;
}
throw new ArgumentOutOfRangeException("Index out of range");
}
OR
private ManagementObject GetItem(ManagementObjectCollection collection, int index)
{
//TODO: do null handling
int currentIndex = 0;
foreach (ManagementObject mo in collection)
{
if (currentIndex == index)
{
return mo;
}
currentIndex++;
}
throw new ArgumentOutOfRangeException("Index out of range");
}