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.

Let clauses and transparent identifiers295You don’t need to know everyth<strong>in</strong>g about transparent identifiers, but I’ll teach youenough so that if you see one <strong>in</strong> the language specification you won’t feel like runn<strong>in</strong>gand hid<strong>in</strong>g. You’ll also understand why they’re needed at all—and that’s where anexample will come <strong>in</strong> handy. The let clause is the simplest transformation availablethat uses transparent identifiers.11.4.1 Introduc<strong>in</strong>g an <strong>in</strong>termediate computation with letA let clause simply <strong>in</strong>troduces a new range variable with a value that can be based onother range variables. The syntax is as easy as pie:let identifier = expressionTo expla<strong>in</strong> this operator <strong>in</strong> terms that don’t use any other complicated operators, I’mgo<strong>in</strong>g to resort to a very artificial example. Suspend your disbelief, and imag<strong>in</strong>e thatf<strong>in</strong>d<strong>in</strong>g the length of a str<strong>in</strong>g is a costly operation. Now imag<strong>in</strong>e that we had a completelybizarre system requirement to order our users by the lengths of their names, andthen display the name and its length. Yes, I know it’s somewhat unlikely. List<strong>in</strong>g 11.10shows one way of do<strong>in</strong>g this without a let clause.List<strong>in</strong>g 11.10Sort<strong>in</strong>g by the lengths of user names without a let clausevar query = from user <strong>in</strong> SampleData.AllUsersorderby user.Name.Lengthselect user.Name;foreach (var name <strong>in</strong> query){Console.WriteL<strong>in</strong>e("{0}: {1}", name.Length, name);}That works f<strong>in</strong>e, but it uses the dreaded Length property twice—once to sort the users,and once <strong>in</strong> the display side. Surely not even the fastest supercomputer could cope withf<strong>in</strong>d<strong>in</strong>g the lengths of six str<strong>in</strong>gs twice! No, we need to avoid that redundant computation.We can do so with the let clause, which evaluates an expression and <strong>in</strong>troducesit as a new range variable. List<strong>in</strong>g 11.11 achieves the same result as list<strong>in</strong>g 11.10, but onlyuses the Length property once per user.List<strong>in</strong>g 11.11Us<strong>in</strong>g a let clause to remove redundant calculationsvar query = from user <strong>in</strong> SampleData.AllUserslet length = user.Name.Lengthorderby lengthselect new { Name = user.Name, Length = length };foreach (var entry <strong>in</strong> query){Console.WriteL<strong>in</strong>e("{0}: {1}", entry.Length, entry.Name);}List<strong>in</strong>g 11.11 <strong>in</strong>troduces a new range variable called length, which conta<strong>in</strong>s the lengthof the user’s name (for the current user <strong>in</strong> the orig<strong>in</strong>al sequence). We then use that newLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!