Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- foreach(object o in collection) {
- return o;
- }
- IEnumerator en = collection.GetEnumerator();
- en.MoveNext();
- return en.Current;
- 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;
- }
- 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());
Add Comment
Please, Sign In to add comment