Why is Predicate<> sealed?
c#, predicate
Solution
`Predicate<T>` is a delegate type. You can never derive from delegates.
To be honest, it doesn't sound like inheritance is really appropriate here anyway - just write a method which returns an inverse of the original. It's as simple as this:
public static Predicate<T> Invert<T>(Predicate<T> original)
{
return t => !original(t);
}
Problem
I wanted to derive a class from Predicate<IMyInterface>, but it appears as if Predicate<> is sealed. In my case I wanted to simply return the inverted (!) result of the designated function. I have other ways to accomplish the goal. My question is what might the MS designers have been thinking when deciding to seal Predicate<>? Without much thought I came up with: (a) simplified their testing, just a time vs cost trade off (b) "no good" could come from deriving from Predicate<> What do you think? Update: There are n predicates that are dynamically added to a list of Predicates during an initialization phase. Each is mutually exclusive (if Abc is added, NotAbc wont be added). I observed a pattern that looks like: ``` bool Happy(IMyInterface I) {...} bool NotHappy(IMyInterface I) { return !Happy(I); } bool Hungry(IMyInterface I) {...} bool NotHungry(IMyInterface I) { return !Hungry(I); } bool Busy(IMyInterface I) {...} bool NotBusy(IMyInterface I) { return !Busy(I); } bool Smart(IMyInterface I) {...} bool NotSmart(IMyInterface I) {...} //Not simply !Smart ``` Its not that I can't solve the problem, its that I wonder why I couldn't solve it a certain way.