Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Latest commit

 

History

History
24 lines (19 loc) · 864 Bytes

2010-08-27-Spot-the-bug.md

File metadata and controls

24 lines (19 loc) · 864 Bytes
layout status title slug
post
publish
Spot the bug
Spot-the-bug

What is wrong with the code below?

Safe Assumptions:

  1. _dictionary is a valid non-null Dictionary<object,object>

  2. _dictionary contains items that will match the passed expression

  3. This code compiles with no warnings or errors

  4. This code will throw an exception at runtime.

    public IList<TModel> GetAllByCriteria<TModel> ( Expression<Func<TModel, bool>> criteria ) { Func<TModel, bool> action = criteria.Compile(); return _dictionary.Where( pair => action( (TModel)pair.Value ) ).Cast<TModel>().ToList(); }

If you don’t see it right away then you’re not alone. I spent a while debugging to catch this. If you do see it right away then pat yourself on the back.