@CAsBsfAAfAfCBDAfXBWC| 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 Microsoft 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.`List.TrimExcess: Microsoft Docs `https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.trimexcess`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 ZDZ; { XZ$X{ XX^Zu^<Z}> ZtZzZwZu<Z}>(); XXZt.Zn^2^); XXZt.Zn^3^); XXZt.Zn^5^); XXZt.Zn^7^); X} }^ Z ZDZ; { XZ$X{ XX^Zu^<Z}> ZtZzZwZu<Z}>(); XXZt.Zn^2^); XXZt.Zn^3^); XXZt.Zn^7^);^ XXZ{Z]through Zu with Z7. XX^Z7^ (ZsprimeZkZt) XX{Z'ZO.Z%prime); XX} X} } ^ 2 3 7^ ZPZO; Z ZDZ; { XZ$X{ XX^Zu^<Z}> ZtZzZwZu<Z}>(ZwZ}[]{ 2, 3, 7 });^ XXZ{Z]with Zoand use ZL Z}erpolationZlZ_ Zds. XX^Z|^ (ZsiZz0; i < Zt.^ZW^; i++) XX{Z'Z%^$"{i}Zz{Zt[i]}"^); XX} X} } ^ 0Zz2 1Zz3 2Zz7^ ZPZO; Z ZDZ; { XZ$X{ XX^Zu^<bool> ZtZzZwZu<bool>(); XXZt.Zn^true^); XXZt.Zn^false^); XXZt.Zn^true^); XXZ%Zt.^ZW^);^ Z{3 XX^Zt.^Clear^(); XXZ%Zt.ZW);^ Z{0 X^} } ^ 3 0^ ZPZO; Z ZDZ; { XZ$X{ XXZ}[] arrZzZwZ}[3];^ Z{New ZT with 3 Z6s. XX^arr[0]Zz^2^; XXarr[1]Zz^3^; XXarr[2]Zz^5^; XX^Zu^<Z}> ZtZzZwZu<Z}>(arr);^ Z{CopyZlZu. XX^Z%Zt.ZW);^X Z{3 Z6sZkZu. X^} } ^ 3^ ZPZO; Z ZDZ; { XZ$X{^ XXZ{New Zt ZoeZQ. XX^Zu<Z}> primesZzZwZu<Z}>(ZwZ}[] { 2, 3, 5 });^ XXZ{See if Zu cZ9 3. XX^Z7^ (ZsZGZkprimes) XX{Z'^if^ (ZGZi3)^ Z{Will match once.Z'^{Z'XZ%^"CZ9 3"^);Z'} XX} X} } ^ CZ9 3^ ZPZO; Z ZDZ; { XZ$X{ XXZu<Z}> primesZzZwZu<Z}>(ZwZ}[] { 19, 23, 29 })ZSXXZsindexZzprimes.^Z\Of^(^23^);^ Z{Exists. XX^Z%index)ZSXXindexZzprimes.^Z\Of^(^10^);^ Z{Does not exist. XX^Z%index); X} } ^ 1 -1^ ZPZO; Z ZDZ; { XZ$X{^ XXZ{Zu of cities we needZljoin. XX^Zu<ZL> citiesZzZwZu<ZL>(); XXcities.Zn^"New York"^); XXcities.Zn^"Mumbai"^); XXcities.Zn^"Berlin"^); XXcities.Zn^"Istanbul"^);^ XXZ{Join ZLs Z}o one CSV line. XX^ZL lineZzZL.^Join^(^","^, cities.ToZU()); XXZ%line); X} } ^ New York,Mumbai,Berlin,Istanbul^ ZPZO; Z ZDZ; { XZ$X{^ XXZ{Populate eZQ DZ+. XX^var dictZzZwDZ+<Z}, bool>(); XXdict.Zn^3^, true); XXdict.Zn^5^, false);^ XXZ{Zqa Zu of all the Keys. XX^Zu<Z}> keysZzZwZu<Z}>(dict.^Keys^); XXZ7 (ZskeyZkkeys) XX{Z'Z%key); XX} X} } ^ 3, 5^ ZPZO; Z ZDZ; { XZ$X{ XXZu<ZL> dogsZzZwZu<ZL>();^ Z{EZQ Zt. XX^dogs.Zn^"spaniel"^);^ Z{CZ9: spaniel. XX^dogs.Zn^"beagle"^);^ Z{CZ9: spaniel, beagle. XX^dogs.^Insert^(1, ^"dalmatian"^);^ Z{Spaniel, dalmatian, beagle. XX^Z7 (ZL dogZkdogs)^ Z{Z5 Zoverification. XX^{Z'Z%dog); XX} X} } ^ spaniel dalmatian beagle^ ZPZO; Z ZDZ; { XZ$X{ XX^Zu<ZL>^ ZtZzZwZu<ZL>(); XXZt.Zn^"anchovy"^); XXZt.Zn^"barracuda"^); XXZt.Zn^"bass"^); XXZt.Zn^"viperfish"^);^ XXZ{Reverse Zu in-place, no Zwvariables required. XX^Zt.^Reverse^()ZSXXZ7 (ZL ZdZkZt) XX{Z'Z%Zd); XX} X} } ^ viperfish bass barracuda anchovy^ ZPZO; Z ZDZ; { XZ$X{ XX^Zu<ZL>^ riversZzZwZu<ZL>(ZwZL[] XX{Z'^"nile"^,Z'^"amazon"^,^ Z{River 2.Z'^"yangtze"^,^ Z{River 3.Z'^"mississippi"^,Z'^"yellow"^ XX});^ XXZ{Zqrivers 2 through 3. XX^Zu<ZL>^ rangeZzrivers.^GetRange^(1, 2); XXZ7 (ZL riverZkrange) XX{Z'Z%river); XX} X} } ^ amazon yangtze^ Z ZDZ; { XZ$X{ XX^var^ Zt1ZzZwZu<Z}>();^X Z{Var keyword used. XX^Zu<Z}>^ Zt2ZzZwZu<Z}>();^ Z{This is equivalent. X^} }^

5adds elements to ListbList, foreach-loopXfor-loop+counts List4copies array to ListWforeachWIndexOf*joins List-converts Keys1inserts into ListWReverse5gets ranges from List]var with List