A discussion has come up at work:
We've got a class that has an IList. Fact is an abstract base class, and there are several concrete subclasses (PopulationFact, GdpFact, etc).
Originally we'd query for a given fact in this fashion, i.e. by type:
.Facts.FirstOrDefault(x => x.Year == 2011 && x is GdpFact)
Now, however, the question's been raised whether we should introduce a FactType enum instead to do
.Facts.FirstOrDefault(x => x.Year == 2011 && x.FactType == FactType.Gdp)
The suggestion has been raised because it is supposedly faster. I'll admit that I've not written any tests to try and discern the difference in performance, but I have two questions:
1) Is 'querying on type' like this inherently bad?
2) Isn't adding a FactType enum just superfluous anyway, given that facts are strongly typed?
UPDATE To clarify, this is LINQ to objects and GdpFact:Fact.
UPDATE 2 We've measured using current typical data (4 facts) and the results are in:
lookup on enum: 0.29660000000000003 milliseconds lookup on type: 0.24530000000000002 milliseconds
So type lookup is faster in this context! I will choose my accepted answer carefully.