Pretty much, yes; but it depends on how your code is structured. If
this isn't in a tight loop, then use which ever approach you like: you
will most likely never see an issue, so don't do premature
optimisation. But for certain the pyramid testing (see Jon's example)
is going to be the fastest.
Jon talks about quantified cost of exceptions here:
http://www.yoda.arachsys.com/csharp/exceptions.html
(with follow-up:
http://www.yoda.arachsys.com/csharp/exceptions2.html)
This at least gives you some figures. As for reflection; following are
timings (ms) taken from 250k get/set pairs against a simple class
(actually it implements INotifyPropertyChanged for my purposes, but
there are no subscribers) with a checksum to ensure no compiler
optimisations; this shows the absolute cost of reflection (don't trust
the 0; obviously this is too tight a scale for just DateTime, but I'm
not going to bother changing it). For comparison, I have also included
some stats for my own "property-bag" implementation, which stores data
in a dictionary rather than fields and provides custom
PropertyDescriptor (via ICustomTypeDescriptor)... this improves
PropertyDescriptor (component-model) access, but at the cost of
levelling direct access... PropertyInfo (reflection) access is still
very slow though, but the aim here was to improve UI-binding access;
job done I believe ;-p
Simple class [250000]
Direct read 0
Direct write 40.0576
PropDesc read 5618.0784
PropDesc write 6669.5904
PropInfo read 4546.5376
PropInfo write 5688.1792
PropertyBag class [250000]
Direct read 260.3744
Direct write 400.576
PropDesc read 290.4176
PropDesc write 420.6048
PropInfo read 5457.848
PropInfo write 6899.9216
Marc