aAaBEeAAXZCB| 6884954V~~~ 688465954VYWBW-~CZ

Convert List, array.` A List can be converted to an array. The opposite conversion is also possible. This is required in many programs—it is important to have it handy.`More complex methods` can be implemented. A for-loop can copy elements from a List and add them to an array. But this is more work for the programmer.`List, array.` There are 2 parts to this example. First we create a new List and populate it with some strings. The List here can only hold strings. Next, we use ToArray on the List. `ToArray `toarray`To make sure it works, we pass the string array to a Test() method. The program prints the array's length.`String Array `array`Array, List.` We can convert an array of any number of elements to a List that has the same type of elements. There are 3 parts to this example. `Part 1: `First, in part 1 the example initializes a new string array containing five strings.`Part 2: `Here we convert the array to a List with the List constructor. This returns a new List of strings.`Part 3: `Here the example converts the array to a List with the ToList() instance method.`Notes, extension method.` The ToList method is an extension method from System.Linq. This kind of method is an addition to normal C# method calls. `ToList `tolist`Extension `extension`Internals.` You can use the IL Disassembler tool provided by Microsoft to look inside methods. The ToList method calls the List constructor. `So: `That means parts 2 and 3 above are the same except for a check against the null literal.`IL Disassembler Tutorial `il-disassembler`Implicit conversion error.` You may get the "Cannot implicitly convert type" error. This error raised by the compiler tells you that the type needs a regular cast or is not compatible. `If your code statement tries to assign a List to an array, you will get this error.`Readability.` It is easy to remember that ToList converts a collection to a List, and ToArray does the same for an array. These methods are well-named. `Performance: `I have not performed extensive micro-benchmarks on these conversion methods.`The new List constructor would be the fastest for that example. A loop that copies elements would be similar.`A short summary.` Convert your arrays to Lists and vice versa with this code. You can't simply assign one to the other. The List constructor can also be used (and it avoids a null check). `List `list

EOIRNIIODOQ;IO IOJOAI{IEO%E{NIEEO{CO_ On.IEENOo<OP> OnOyOqOo<OP>();IEEOn.OvNRoneRN);IEEOn.OvNRtwoRN);IEEOn.OvNRthreeRN);IEEOn.OvNRfourRN);IEEOn.OvNRfiveRN);NIIEEO{CONOjOP OU.IEENOP[] OUOyOn.NToOSN();IEETest(OU);IE}IIEO?OcTest(NOP[]N OU)IE{IEEO'NROS received: RNO}OU.LO^);IE}I}IINIIOS received: 5NIIODOQ;IO ODOQ.LinqObOJOAI{IEO%E{NIEEO{OO OU (part 1).IEENOP[] OUOyOqOP[]IEE{IEEENRoneRN,IEEENRtwoRN,IEEENRthreeRN,IEEENRfourRN,IEEENRfiveRNIEE};NIEEO{Use On constructor (part 2).IEENOo<OP> On1OyNOqOo<OP>N(OU);IEETest(On1);NIIEEO{Use ToOo mOg (part 3).IEENOo<OP> On2OyOU.NToOoN();IEETest(On2);IE}IIEO?OcTest(NOo<OP>N On)IE{IEEO'NROo count: RNO}On.O]);IE}I}IINIIOo count: 5IOo count: 5N

"WkToArrayList constructor and ToList