views:

3846

answers:

3

What is the best way to get a value from a ICollection? We know the Collection is empty apart from that.

+10  A: 

The simplest way to do this is:

foreach(object o in collection) {
  return o;
}

But this isn't particularly efficient if it's actually a generic collection because IEnumerator implements IDisposable, so the compiler has to put in a try/finally, with a Dispose() call in the finally block.

If it's a non-generic collection, or you know the generic collection implements nothing in its Dispose() method, then the following can be used:

IEnumerator en = collection.GetEnumerator();
en.MoveNext();
return en.Current;

If you know if may implement IList, you can do this:

IList iList = collection as IList;
if (iList != null) {
  // Implements IList, so can use indexer
  return iList[0];
}
// Use the slower way
foreach (object o in collection) {
  return o;
}

Likewise, if it's likely it'll be of a certain type of your own definition that has some kind of indexed access, you can use the same technique.

Chris
+1  A: 

Without generics and because ICollection implements IEnumerable you can do like in example 1. With generics you simple need to do like example 2:

List<string> l = new List<string>();
l.Add("astring");

ICollection col1 = (ICollection)l;
ICollection<string> col2 = (ICollection<string>)l;

//example 1
IEnumerator e1 = col1.GetEnumerator();
if (e1.MoveNext())
    Console.WriteLine(e1.Current);

//example 2
if (col2.Count != 0)
    Console.WriteLine(col2.Single());
bruno conde
example 2 isn't right because ICollection doesn't implement method Single().
This is a Linq extension method. Take a look at msdn doc : http://msdn.microsoft.com/en-us/library/y2fx0ty0.aspx
bruno conde
+8  A: 

Linq, baby, yeah...

   var foo = myICollection.OfType<YourType>().FirstOrDefault();
    // or use a query
    var bar = (from x in myICollection.OfType<YourType>() where x.SomeProperty = someValue select x)
       .FirstOrDefault();
Will
FirstOrDefault is defined on ICollection or IEnumerable (non-generic)? Where?What would work though: myICollection.OfType<YourType>().FirstOrDefault();
Christoph Rüegg
True, updated. Thanks.
Will
This is the right answer. First and FirstOrDefault even use that IList optimisation internally where possible.
Daniel Earwicker