# Tutorial by Examples

## Intersect

var numbers1to10 = new[] {1,2,3,4,5,6,7,8,9,10}; var numbers5to15 = new[] {5,6,7,8,9,10,11,12,13,14,15}; var numbers5to10 = numbers1to10.Intersect(numbers5to15); Console.WriteLine(string.Join(&quot;,&quot;, numbers5to10)); //5,6,7,8,9,10

## Concat

var numbers1to5 = new[] {1, 2, 3, 4, 5}; var numbers4to8 = new[] {4, 5, 6, 7, 8}; var numbers1to8 = numbers1to5.Concat(numbers4to8); Console.WriteLine(string.Join(&quot;,&quot;, numbers1to8)); //1,2,3,4,5,4,5,6,7,8 Note that duplicates are kept in the result. If this is undesirable, use...

## First (find)

var numbers = new[] {1,2,3,4,5}; var firstNumber = numbers.First(); Console.WriteLine(firstNumber); //1 var firstEvenNumber = numbers.First(n =&gt; (n &amp; 1) == 0); Console.WriteLine(firstEvenNumber); //2 The following throws InvalidOperationException with message &quot;Sequence contain...

## Single

var oneNumber = new[] {5}; var theOnlyNumber = oneNumber.Single(); Console.WriteLine(theOnlyNumber); //5 var numbers = new[] {1,2,3,4,5}; var theOnlyNumberSmallerThanTwo = numbers.Single(n =&gt; n &lt; 2); Console.WriteLine(theOnlyNumberSmallerThanTwo); //1 The following throws Invalid...

## Last

var numbers = new[] {1,2,3,4,5}; var lastNumber = numbers.Last(); Console.WriteLine(lastNumber); //5 var lastEvenNumber = numbers.Last(n =&gt; (n &amp; 1) == 0); Console.WriteLine(lastEvenNumber); //4 The following throws InvalidOperationException: var lastNegativeNumber = numbers.Last(n...

## LastOrDefault

var numbers = new[] {1,2,3,4,5}; var lastNumber = numbers.LastOrDefault(); Console.WriteLine(lastNumber); //5 var lastEvenNumber = numbers.LastOrDefault(n =&gt; (n &amp; 1) == 0); Console.WriteLine(lastEvenNumber); //4 var lastNegativeNumber = numbers.LastOrDefault(n =&gt; n &lt; 0); Con...

## SingleOrDefault

var oneNumber = new[] {5}; var theOnlyNumber = oneNumber.SingleOrDefault(); Console.WriteLine(theOnlyNumber); //5 var numbers = new[] {1,2,3,4,5}; var theOnlyNumberSmallerThanTwo = numbers.SingleOrDefault(n =&gt; n &lt; 2); Console.WriteLine(theOnlyNumberSmallerThanTwo); //1 var theOnl...

## FirstOrDefault

var numbers = new[] {1,2,3,4,5}; var firstNumber = numbers.FirstOrDefault(); Console.WriteLine(firstNumber); //1 var firstEvenNumber = numbers.FirstOrDefault(n =&gt; (n &amp; 1) == 0); Console.WriteLine(firstEvenNumber); //2 var firstNegativeNumber = numbers.FirstOrDefault(n =&gt; n &lt; ...

## Any

Returns true if the collection has any elements that meets the condition in the lambda expression: var numbers = new[] {1,2,3,4,5}; var isNotEmpty = numbers.Any(); Console.WriteLine(isNotEmpty); //True var anyNumberIsOne = numbers.Any(n =&gt; n == 1); Console.WriteLine(anyNumberIsOne); //Tr...

## All

var numbers = new[] {1,2,3,4,5}; var allNumbersAreOdd = numbers.All(n =&gt; (n &amp; 1) == 1); Console.WriteLine(allNumbersAreOdd); //False var allNumbersArePositive = numbers.All(n =&gt; n &gt; 0); Console.WriteLine(allNumbersArePositive); //True Note that the All method functions by che...

## default

For classes, interfaces, delegate, array, nullable (such as int?) and pointer types, default(TheType) returns null: class MyClass {} Debug.Assert(default(MyClass) == null); Debug.Assert(default(string) == null); For structs and enums, default(TheType) returns the same as new TheType(): struct...

The readonly keyword is a field modifier. When a field declaration includes a readonly modifier, assignments to that field can only occur as part of the declaration or in a constructor in the same class. The readonly keyword is different from the const keyword. A const field can only be initialized...

## SelectMany (flat map)

Enumerable.Select returns an output element for every input element. Whereas Enumerable.SelectMany produces a variable number of output elements for each input element. This means that the output sequence may contain more or fewer elements than were in the input sequence. Lambda expressions passe...

## Sum

var numbers = new[] {1,2,3,4}; var sumOfAllNumbers = numbers.Sum(); Console.WriteLine(sumOfAllNumbers); //10 var cities = new[] { new {Population = 1000}, new {Population = 2500}, new {Population = 4000} }; var totalPopulation = cities.Sum(c =&gt; c.Population); Console.Wr...

## Skip

Skip will enumerate the first N items without returning them. Once item number N+1 is reached, Skip starts returning every enumerated item: var numbers = new[] {1,2,3,4,5}; var allNumbersExceptFirstTwo = numbers.Skip(2); Console.WriteLine(string.Join(&quot;,&quot;, allNumbersExceptFirstTwo.ToA...

## Take

This method takes the first n elements from an enumerable. var numbers = new[] {1,2,3,4,5}; var threeFirstNumbers = numbers.Take(3); Console.WriteLine(string.Join(&quot;,&quot;, threeFirstNumbers.ToArray())); //1,2,3

## SequenceEqual

var numbers = new[] {1,2,3,4,5}; var sameNumbers = new[] {1,2,3,4,5}; var sameNumbersInDifferentOrder = new[] {5,1,4,2,3}; var equalIfSameOrder = numbers.SequenceEqual(sameNumbers); Console.WriteLine(equalIfSameOrder); //True var equalIfDifferentOrder = numbers.SequenceEqual(sameNumbersInDi...

## Reverse

var numbers = new[] {1,2,3,4,5}; var reversed = numbers.Reverse(); Console.WriteLine(string.Join(&quot;,&quot;, reversed.ToArray())); //5,4,3,2,1

## OfType

var mixed = new object[] {1,&quot;Foo&quot;,2,&quot;Bar&quot;,3,&quot;Fizz&quot;,4,&quot;Buzz&quot;}; var numbers = mixed.OfType&lt;int&gt;(); Console.WriteLine(string.Join(&quot;,&quot;, numbers.ToArray())); //1,2,3,4

## Max

var numbers = new[] {1,2,3,4}; var maxNumber = numbers.Max(); Console.WriteLine(maxNumber); //4 var cities = new[] { new {Population = 1000}, new {Population = 2500}, new {Population = 4000} }; var maxPopulation = cities.Max(c =&gt; c.Population); Console.WriteLine(maxPopu...

Page 5 of 1336