Saturday, July 28, 2012

Pass NULL to the Contains Method in C#

I wanted to use FirstOrDefault method inside of the Contains method for List<T>. FirstOrDefault(...) method returns the first appearance of the element (defined by the expression in the brackets) or the default element. Default element is the result of default(SomeClass) and is NULL for all user-defined classes. Thus, my concerns was what would happen if I will pass NULL to the Contains method. It happens that Contains just returns FALSE wherever it has NULL as an input parameter. It does not depend on whether are any elements in the collection.

No comments:

Post a Comment