13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Life <strong>in</strong> a parallel universe357ArrayList filteredUsers = new ArrayList();foreach (User user <strong>in</strong> SampleData.AllUsers){if (user.UserType==UserType.Developer){filteredUsers.Add(user);}}filteredUsers.Sort(new UserNameComparer());ArrayList upperCasedNames = new ArrayList();foreach (User user <strong>in</strong> filteredUsers){upperCasedNames.Add(user.Name.ToUpper());}Each step is clear, but it’s relatively hard to understand exactly what’s go<strong>in</strong>g on! Theversion we saw earlier with the explicit calls to Enumerable was shorter, but the evaluationorder still made it difficult to read. <strong>C#</strong> 3 hides exactly how and where the filter<strong>in</strong>g,sort<strong>in</strong>g, and projection is tak<strong>in</strong>g place—even after translat<strong>in</strong>g the query expression<strong>in</strong>to method calls—but the overall purpose of the code is much more obvious.Usually this type of readability is a good th<strong>in</strong>g, but it does mean you need to keepyour wits about you. For <strong>in</strong>stance, captur<strong>in</strong>g local variables makes it a lot easier towrite query expressions—but you need to understand that if you change the values ofthose local variables after creat<strong>in</strong>g the query expression, those changes will applywhen you execute the query expression.One of the aims of this book has been to make you sufficiently comfortable withthe mechanics of <strong>C#</strong> 3 that you can make use of the magic without f<strong>in</strong>d<strong>in</strong>g it hard tounderstand what’s go<strong>in</strong>g on when you need to dig <strong>in</strong>to it—as well as warn<strong>in</strong>g you ofsome of the potential hazards you might run <strong>in</strong>to.So far these have all been somewhat <strong>in</strong>ward-look<strong>in</strong>g aspects of development—changes that could have happened at any time. The next po<strong>in</strong>t is very much due towhat a biologist might call an “external stimulus.”13.4 Life <strong>in</strong> a parallel universeIn chapter 12 we looked briefly at Parallel LINQ, and I mentioned that it is part of awider project called Parallel Extensions. This is Microsoft’s next attempt to make concurrencyeasier. I don’t expect it to be the f<strong>in</strong>al word on such a daunt<strong>in</strong>g topic, but it’sexcit<strong>in</strong>g nonetheless.As I write this, most computers still have just a few cores. Some servers have eightor possibly even 16 (with<strong>in</strong> the x86/x64 space—other architectures already supportfar more than this). Given how everyth<strong>in</strong>g <strong>in</strong> the <strong>in</strong>dustry is progress<strong>in</strong>g, it may not belong before that looks like small fry, with genu<strong>in</strong>e massively parallel chips becom<strong>in</strong>gpart of everyday life. Concurrency is at the tipp<strong>in</strong>g po<strong>in</strong>t between “nice to have” and“must have” as a developer skill.We’ve already seen how the functional aspects of <strong>C#</strong> 3 and LINQ enable some concurrencyscenarios—parallelism is often a matter of break<strong>in</strong>g down a big task <strong>in</strong>to lotsLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!