[Official Notes]
Adds the elements of the specified collection to the end of the List
namespaces: System.Collections.Generic
Assembly:mscorlib (in mscorlib.dll)
(ienumerable<t>^ Collection)
Collection: A collection whose elements should be added to the end of the List. The collection itself cannot be a null reference (Nothing in Visual Basic),
However, it can contain elements that are null references (nothing in Visual Basic) (if Type T is a reference type).
remark:Listpreserves the order of the elements in the collection.
If the new count (the current count plus the size of the collection) is greater than capacity, the capacity of the List is increased by automatically reallocating the internal array to accommodate the new element, and the existing elements are copied to the new array before the new element is added.
If the List can accommodate new elements without adding capacity , this method is an O (N) operation, where N is the number of elements to be added. If you need to increase this capacity to accommodate the new element, this method becomes an O (n + m) operation, where N is the number of elements to be added, andm is count.
Example:
usingSystem;usingSystem.Collections.Generic; Public classexample{ Public Static voidMain () {string[] input = {"Brachiosaurus", "Amargasaurus", "Mamenchisaurus" }; List<string> Dinosaurs =Newlist<string>(input); Console.WriteLine ("\ncapacity: {0}", dinosaurs. capacity); Console.WriteLine (); foreach(stringDinosaurinchdinosaurs) {Console.WriteLine (dinosaur); } Console.WriteLine ("\naddrange (dinosaurs)"); Dinosaurs. AddRange (dinosaurs); Console.WriteLine (); foreach(stringDinosaurinchdinosaurs) {Console.WriteLine (dinosaur); } Console.WriteLine ("\nremoverange (2, 2)"); Dinosaurs. RemoveRange (2,2); Console.WriteLine (); foreach(stringDinosaurinchdinosaurs) {Console.WriteLine (dinosaur); } Input=New string[] {"Tyrannosaurus", "Deinonychus", "Velociraptor"}; Console.WriteLine ("\ninsertrange (3, input)"); Dinosaurs. Insertrange (3, input); Console.WriteLine (); foreach(stringDinosaurinchdinosaurs) {Console.WriteLine (dinosaur); } Console.WriteLine ("\noutput = dinosaurs. GetRange (2, 3). ToArray ()"); string[] output = dinosaurs. GetRange (2,3). ToArray (); Console.WriteLine (); foreach(stringDinosaurinchoutput) {Console.WriteLine (dinosaur); } }}
"C # Learning Notes" List.addrange method