From the below 2 ways to enumerate a collection, should one be preferred over the other OR are both the same in terms of performance (i.e. since ToList() conversion is involved in the second approach)
foreach(SomeType st in Collection)
{
//Do something
}
Collection.OfType<SomeType>.ToList().ForEach()
Is there any other way (except using relection)?
EDIT
How about this scenario?
class SomeType : IMyType
{
}
class OtherType : IMyType
{
}
class SomeCollection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{
}
Considering MyTypes is a property in a class with type SomeCollection<IMyType>
the below 2 approaches same?
foreach(IMyType mt in MyTypes)
{
if(IMyType is SomeType)
return;
}
OR
if(MyTypes.OfType<SomeType>.Any())
return;