public IEnumerableUsing an explicit cast performs well, but will result in an InvalidCastException if the cast fails.Cast (this IEnumerable source) { foreach(object o in source) yield return (T) o; }
A less efficient yet useful variation on this idea is OfType
public IEnumerableThe returned enumeration will only include elements that can safely be cast to the specified type.OfType (this IEnumerable source) { foreach(object o in source) if(o is T) yield return (T) o; }
0 comments:
Post a Comment