Information Technology Reference
In-Depth Information
The code samples above showed that you sometimes need casts to help
the compiler pick the method you want in many complicated situations.
In the real world, you'll undoubtedly run into situations where you need
to use casts because class hierarchies, implemented interfaces, and exten-
sion methods have conspired to make the method you want, not the
method the compiler picks as the “best” method. But the fact that real-
world situations are occasionally ugly does not mean you should add to the
problem by creating more overloads yourself.
Now you can amaze your friends at programmer cocktail parties with a
more in-depth knowledge of overload resolution in C#. It can be useful
information to have, and the more you know about your chosen language
the better you'll be as a developer. But don't expect your users to have the
same level of knowledge. More importantly, don't rely on everyone having
that kind of detailed knowledge of how overload resolution works to be
able to use your API. Instead, don't overload methods declared in a base
class. It doesn't provide any value, and it will only lead to confusion among
your users.
Item 35: Learn How PLINQ Implements Parallel Algorithms
This is the item where I wish I could say that parallel programming is now
as simple as adding AsParallel() to all your loops. It's not, but PLINQ does
make it much easier than it was to leverage multiple cores in your pro-
grams and still have programs that are correct. It's by no means trivial to
create programs that make use of multiple cores, but PLINQ makes it
easier.
Yo u s t i l l h a v e t o u n d e r s t a n d w h e n d a t a a c c e s s m u s t b e s y n c h r o n i z e d . Yo u
still need to measure the effects of parallel and sequential versions of the
methods declared in ParallelEnumerable. Some of the methods involved
in LINQ queries can execute in parallel very easily. Others force more
sequential access to the sequence of elements—or, at least, require the
complete sequence (like Sort). Let's walk through a few samples using
PLINQ and learn what works well, and where some of the pitfalls still exist.
All the samples and discussions for this item use LINQ to Objects. The
title even calls out “Enumerable,” not “Queryable”. PLINQ really won't
help you parallelize LINQ to SQL, or Entity Framework algorithms. That's
not really a limiting feature, because those implementations leverage the
parallel database engines to execute queries in parallel.
 
 
Search WWH ::




Custom Search