JA%oznyqoznvnvnvnv JA%oznyqoznvnvnv{Wo@@ipnQ'DQ JA%oznyqozqz{WwPzj[h|iyn]'ynyyyDQ JA%onyqonvnvnv'n]{n'n]{DQ JA%zyqz{U8yyyoznyqoz{jo'n]{8poDQ JA%{nwFozyqozqz{o:@iEpEx{'::DQ JA%ozyqozqzbiyT{'byT{'DQ JA%{ojoPyqoPvvvv{PzPyPS'DQ JA%{F-yq-zvv{koozyqoz@ip'DQ JA%oPyqoP{Fnv{:v{:{@Pp{=w'DQ JA%oPnyqoPnvnvnvnv{oqnb@Phpn'hDQ JA%oPyqoPqP{{{koPy@Pp' JA%nyqoz{oznyqoz{aACbAsEBsfAAfAfCBDAfXBWC| 788ZZ- 784G7VC 7G754V~BC~ 7555796VC 684766V 6G7564VC 74946VYBZCCW~BZWW-~C 68894V~ 689VBCB 64649466VBZBZCZ~ 5889VYZYZ 58G4G8G57VBZWBZ 7G56ZZZZZZZX

List.` As millions of years pass, layers of rock are added to the ground. The fossil record is a list. In a program, we could use a List collection to represent this.`The List` in C# is initialized with the new keyword. When we call Add, the List adjusts its size as needed. It is used in nearly all larger C# programs. `Initialize List `initialize-list`First example.` Here we have a list of ints. We add 4 prime numbers to our list. The values are stored in the order added—2, 3, 5 and then 7. `There are other ways to create, and add elements to, Lists—this is not the simplest.`Note 2: `The angle brackets are part of the declaration type. They are not conditional (less or more than) operators.`Note, int.` The above example adds a primitive type (int) to a List collection. But a List can also hold reference types and object instances. Non-int types work just as well. `Add `list-add`AddRange.` For adding many elements at once—adding an array to a List—we use the AddRange method. This can simplify code that combines collections. `AddRange `list-addrange`Foreach-loop.` This is the best loop when no index is needed. We use the "foreach" keyword and declare a variable (like "prime" here) that is assigned to each element as we pass over it. `For-loop.` Sometimes we want to access indexes of a List as we loop over its elements. A for-loop is ideal here. We print each element index with a string interpolation expression. `Arrays use Length. But Lists use Count. To loop backwards, start with list.Count - 1, and decrement to >= 0.`Count, clear.` To get the number of elements, access the Count property. This is fast—just avoid the Count extension method. Count, on the List type, is equal to Length on arrays. `Clear: `Here we use the Clear method, along with the Count property, to erase all the elements in a List.`Clear `list-clear`Info: `Before Clear is called, this List has 3 elements. After Clear is called, it has 0 elements.`Null: `We can assign the List to null instead of calling Clear, with similar performance.`Copy array.` Here we create a List with elements from an array. We use the List constructor and pass it the array. List receives this parameter and fills its values from it. `Caution: `The array element type must match the List element type or compilation will fail.`Test elements.` We test each element for a certain value. This example shows a foreach-loop, which tests to see if 3 is in a list of primes. `IndexOf.` This determines the element index of a certain value in the List collection. It searches for the first position (from the start) of the value. `IndexOf has two overloads. It works in the same way as string's IndexOf. It searches by value and returns the location.`Contains, Exists, Find.` These methods all provide searching. They vary in arguments accepted. With Predicates, we influence what elements match. `Contains `list-contains`Exists `list-exists`Find `list-find`Capacity.` We can use the Capacity property on List, or pass an integer to the constructor (which sets an initial capacity) to improve allocation performance. `Capacity `capacity`Setting a capacity sometimes improves performance by nearly two times for adding elements.`However: `Adding elements, and resizing List, is not usually a performance bottleneck in programs that access data.`TrimExcess.` This method's usage is limited. It reduces the memory used by lists with large capacities. And as MSDN states, TrimExcess often does nothing. `It is unclear how TrimExcess feels about its status. I wouldn't want to upset its feelings.`Quote: `The TrimExcess method does nothing if the list is at more than 90 percent of capacity.`TrimExcess: MSDN `https://msdn.microsoft.com/en-us/library/ms132207.aspx`BinarySearch.` This implements (fittingly) the binary search algorithm. Binary search uses guesses to find the correct element faster than linear searching. `BinarySearch `binarysearch`ForEach.` This is a method. Sometimes we may not want to write a traditional foreach-loop. Here ForEach is useful. It accepts an Action. `Warning: `Be cautious with Predicates and Actions. These objects can decrease the readability of code.`TrueForAll.` This method accepts a Predicate. If the Predicate returns true for each element in the List, TrueForAll() will also return true. `TrueForAll() checks the entire list—unless an element doesn't match (it has an early exit condition).`Join string list.` Next we use string.Join on a List of strings. This is helpful when we need to turn several strings into one comma-delimited string. `ToArray: `It requires the ToArray instance method on List. This ToArray is not an extension method.`The biggest advantage of Join here is that no trailing comma is present on the resulting string.`Keys in Dictionary.` We use the List constructor to get a List of keys from a Dictionary. This is a simple way to iterate over Dictionary keys (or store them elsewhere). `Keys: `The Keys property returns an enumerable collection of keys. But a List of these elements is more usable.`Insert.` This is a useful but slow method. The string here is inserted at index 1. This makes it the second element. If you Insert often, consider Queue and LinkedList. `Insert `list-insert`A Queue may allow simpler usage of the collection in our code. This may be easier to understand.`Queue `queue`InsertRange.` This method inserts many elements at once. Please be aware it can impact performance in a negative way. Successive elements must be copied. `InsertRange `insertrange`Remove.` We present examples for Remove, RemoveAt, RemoveAll and RemoveRange. In general Remove operates the same way as Insert. It too hinders performance. `Remove, RemoveAt `list-remove`RemoveAll `removeall`Sort` orders the elements in the List. For strings it orders alphabetically. For integers (or other numbers) it orders from lowest to highest. `Sort `sort-list`Sort acts upon elements depending on type. It is possible to provide a custom method.`Reverse.` With this method no sorting occurs—the original order is intact but inverted. The strings contained in the List are left unchanged. `Array.Reverse `array-reverse`Internally: `This method invokes the Array.Reverse method. Many list methods are implemented with Array methods.`Conversion` of data types is a challenge. We can convert a List to an array of the same type using the instance method ToArray. There are examples of these conversions. `List to Array `convert-list-array`CopyTo `list-copyto`Convert list to string.` Some string methods are used with lists. We use Concat and Join. Sometimes StringBuilder is also useful. This is a common requirement. `Convert List, String `convert-list-string`Concat `string-concat`Join: string.Join `string-join`GetRange.` This returns a range of elements in a List. This is similar to the Take and Skip methods from LINQ. It has different syntax. The result List can be used like any other List. `LINQ `linq`DataGridView.` We can use the List type with a DataGridView. But sometimes it is better to convert the List to a DataTable. For a List of string arrays, this will work better. `Convert List, DataTable `convert-list-datatable`Equality.` Sometimes we need to test two Lists for equality, even when their elements are unordered. We can sort and then compare, or use a custom List equality method. `List Element Equality `list-equals`Structs.` When using List, we can improve performance and reduce memory usage with structs instead of classes. A List of structs is allocated in contiguous memory, unlike a List of classes. `However: `Using structs will actually decrease performance when they are used as parameters in methods such as those on the List type.`Structs `struct`Var keyword.` This shortens lines of code, which sometimes improves readability. Var has no effect on performance, only readability for programmers. `Var `var`GetEnumerator.` Programs are built upon many abstractions. With List, even loops can be abstracted (into an Enumerator). We use the same methods to loop over a List or an array. `GetEnumerator `getenumerator`Combine lists.` With Concat, a method from the System.Linq namespace, we can add one list to another. Only a single method call is required. `Concat, List `concat`Remove duplicates.` With Distinct() we can remove duplicates from a List. Other algorithms, that use Dictionary, can be used to scan for and erase duplicates. `Dedupe List `duplicates`Serialize list.` A List can be read from, and written to, a file. This is list serialization. The "Serializable" attribute is useful here. `Serialize List `serialize-list`Nested list.` With nesting, we create jagged lists. We can simulate 2D lists with lists of lists. This is powerful and often useful. `Nested List `nested-list`Null.` The List is a reference type—it is allocated on the managed heap. And it can be null. We often must check for null lists to avoid NullReferenceExceptions. `Null List `null-list`Static.` With a static list, we form a global store of elements. A public, static list can be accessed throughout a program with no object instances. `Static List `static-list`List is a constructed,` parametric type. It is powerful and performs well. It provides flexible allocation and growth, making it easier to use than arrays.`List's syntax` is at first confusing. But we become used to it. In most programs lacking strict memory or performance constraints, List is ideal.

XZ^ Z ZZ { XZX{ XX^Z^<Z> ZZZZ<Z>(); XXZ.Z^2^); XXZ.Z^3^); XXZ.Z^5^); XXZ.Z^7^); X} }^ Z ZZ { XZX{ XX^Z^<Z> ZZZZ<Z>(); XXZ.Z^2^); XXZ.Z^3^); XXZ.Z^7^);^ XXZZthrough Z with Z. XX^Z^ (ZprimeZZ) XX{ XXXZ.Zprime); XX} X} } ^ 2 3 7^ ZZ; Z ZZ { XZX{ XX^Z^<Z> ZZZZ<Z>(ZZ[]{ 2, 3, 7 });^ XXZZwith Zand use Z ZerpolationZZ Zs. XX^Z^ (ZiZ0; i < Z.^Z^; i++) XX{ XXXZ^$"{i}Z{Z[i]}"^); XX} X} } ^ 0Z2 1Z3 2Z7^ ZZ; Z ZZ { XZX{ XX^Z^<bool> ZZZZ<bool>(); XXZ.Z^true^); XXZ.Z^false^); XXZ.Z^true^); XXZZ.^Z^);^ Z3 XX^Z.^Clear^(); XXZZ.Z);^ Z0 X^} } ^ 3 0^ ZZ; Z ZZ { XZX{ XXZ[] arrZZZ[3];^ ZNew Z with 3 Zs. XX^arr[0]Z^2^; XXarr[1]Z^3^; XXarr[2]Z^5^; XX^Z^<Z> ZZZZ<Z>(arr);^ ZCopyZZ. XX^ZZ.Z);^X Z3 ZsZZ. X^} } ^ 3^ ZZ; Z ZZ { XZX{^ XXZNew Z ZeZ. XX^Z<Z> primesZZZ<Z>(ZZ[] { 2, 3, 5 });^ XXZSee if Z cZ 3. XX^Z^ (ZZZprimes) XX{ XXX^if^ (ZZ3)^ ZWill match once. XXX^{ XXXXZ^"CZ 3"^); XXX} XX} X} } ^ CZ 3^ ZZ; Z ZZ { XZX{ XXZ<Z> primesZZZ<Z>(ZZ[] { 19, 23, 29 })ZXXZindexZprimes.^ZOf^(^23^);^ ZExists. XX^Zindex)ZXXindexZprimes.^ZOf^(^10^);^ ZDoes not exist. XX^Zindex); X} } ^ 1 -1^ ZZ; Z ZZ { XZX{^ XXZZ of cities we needZjoin. XX^Z<Z> citiesZZZ<Z>(); XXcities.Z^"New York"^); XXcities.Z^"Mumbai"^); XXcities.Z^"Berlin"^); XXcities.Z^"Istanbul"^);^ XXZJoin Zs Zo one CSV line. XX^Z lineZZ.^Join^(^","^, cities.ToZ()); XXZline); X} } ^ New York,Mumbai,Berlin,Istanbul^ ZZ; Z ZZ { XZX{^ XXZPopulate eZ DZ. XX^var dictZZDZ<Z, bool>(); XXdict.Z^3^, true); XXdict.Z^5^, false);^ XXZZa Z of all the Keys. XX^Z<Z> keysZZZ<Z>(dict.^Keys^); XXZ (ZkeyZkeys) XX{ XXXZkey); XX} X} } ^ 3, 5^ ZZ; Z ZZ { XZX{ XXZ<Z> dogsZZZ<Z>();^ ZEZ Z. XX^dogs.Z^"spaniel"^);^ ZCZ: spaniel. XX^dogs.Z^"beagle"^);^ ZCZ: spaniel, beagle. XX^dogs.^Insert^(1, ^"dalmatian"^);^ ZSpaniel, dalmatian, beagle. XX^Z (Z dogZdogs)^ ZZ Zverification. XX^{ XXXZdog); XX} X} } ^ spaniel dalmatian beagle^ ZZ; Z ZZ { XZX{ XX^Z<Z>^ ZZZZ<Z>(); XXZ.Z^"anchovy"^); XXZ.Z^"barracuda"^); XXZ.Z^"bass"^); XXZ.Z^"viperfish"^);^ XXZReverse Z in-place, no Zvariables required. XX^Z.^Reverse^()ZXXZ (Z ZZZ) XX{ XXXZZ); XX} X} } ^ viperfish bass barracuda anchovy^ ZZ; Z ZZ { XZX{ XX^Z<Z>^ riversZZZ<Z>(ZZ[] XX{ XXX^"nile"^, XXX^"amazon"^,^ ZRiver 2. XXX^"yangtze"^,^ ZRiver 3. XXX^"mississippi"^, XXX^"yellow"^ XX});^ XXZZrivers 2 through 3. XX^Z<Z>^ rangeZrivers.^GetRange^(1, 2); XXZ (Z riverZrange) XX{ XXXZriver); XX} X} } ^ amazon yangtze^ Z ZZ { XZX{ XX^var^ Z1ZZZ<Z>();^X ZVar keyword used. XX^Z<Z>^ Z2ZZZ<Z>();^ ZThis is equivalent. X^} }^

-5bX+4WW*-1W5]adds elements to ListList, foreach-loopfor-loopcounts Listcopies array to ListforeachIndexOfjoins Listconverts Keysinserts into ListReversegets ranges from Listvar with List