Having a collection IEnumerable<Nullable<T>> myCollection
you can select values that are not null
by calling myCollection.OfType<T>()
. What is not so obvious is that calling myCollection.OfType<Nullable<T>>()
would provide the same result.
This is because Enumerable.OfType<TResult>
uses is
internally to determine the type of item. It means, you can use Enumerable.OfType<T>()
even for reference type T
to get items that are not null
.
Of course, I would prefer calling Where<T>(item => (item != null))
to calling OfType<T>()
because the first call is much better in revealing its intention.
Monday, April 6, 2009
Selecting not nulls?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment