13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

364 APPENDIX LINQ standard query operatorsTable A.5Sequence equality examplesExpressionwords.SequenceEqual(new[]{"zero","one","two","three","four"})words.SequenceEqual(new[]{"ZERO","ONE","TWO","THREE","FOUR"})words.SequenceEqual(new[]{"ZERO","ONE","TWO","THREE","FOUR"},Str<strong>in</strong>gComparer.Ord<strong>in</strong>alIgnoreCase)ResultTrueFalseTrueA.6 GenerationOut of all the generation operators (see table A.6), only one acts on an exist<strong>in</strong>gsequence: DefaultIfEmpty. This returns either the orig<strong>in</strong>al sequence if it’s not empty,or a sequence with a s<strong>in</strong>gle element otherwise. The element is normally the defaultvalue for the sequence type, but an overload allows you to specify which value to use.There are three other generation operators that are just static methods <strong>in</strong>Enumerable:■ Range generates a sequence of <strong>in</strong>tegers, with the parameters specify<strong>in</strong>g the firstvalue and how many values to generate.■ Repeat generates a sequence of any type by repeat<strong>in</strong>g a specified s<strong>in</strong>gle valuefor a specified number of times.■ Empty generates an empty sequence of any type.All of the generation operators use deferred execution.Table A.6Generation examplesExpressionResultnumbers.DefaultIfEmpty() 0, 1, 2, 3, 4new <strong>in</strong>t[0].DefaultIfEmpty()new <strong>in</strong>t[0].DefaultIfEmpty(10)0 (with<strong>in</strong> an IEnumerable)10 (with<strong>in</strong> an IEnumerable)Enumerable.Range(15, 2) 15, 16Enumerable.Repeat(25, 2) 25, 25Enumerable.Empty()An empty IEnumerableLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!