Skip to main content

Practice ยท 3 of 3

Compile once, cache forever

CHALLENGE
Difficulty: advanced+25 XP

Implement static Func<int, bool> CachedCheck(int threshold) that compiles the predicate x => x >= threshold && x % 2 == 0 for the given threshold and caches the delegate in a static ConcurrentDictionary<int, Func<int, bool>>. The test proves compilation happens once: the SECOND call with the same threshold must return the SAME delegate instance (ReferenceEquals), invoking the cached delegate allocates nothing, and the boundary is exact โ€” CachedCheck(10) accepts 10.

Back to lesson: Practice: Expression drills