C# List

List< /span>

List T is the element type in the list, compile The browser will check if the element type you add to the list is correct. If it is not correct, an error will be reported. In other words, the List is type safe.
List listOfStrings = new List();
List is very similar to Array (they both seem to pass IList interface), but in most cases, list is better than Arrays Use
  . For example, you don’t need to specify the number of elements, just add them. Net will automatically correct the number for us after adding, but note that the type of the added item must be the type represented by T
 Share picture

initialization
share picture

Similarly use curly braces to separate multiple elements with commas

Share a picture

The above example is similar in structure to the Arrays example in the previous section. Pay attention to the use of List


Adding Add element
Insert (index, item) Insert an element at the specified position, the original element and all subsequent elements are moved back by one place< /span>
AddRange(xxx) xxx can be any type of collection array or list (of course the data types of the elements are still the same, string to string, int For int)
InsertRange(index,xxx)xxx refer to the above

Remove(“John”) Search for John from the beginning, and throw him out of the list if you find it. If you can’t find it, no error will be reported. The return value is TRUE. Throw away, FALSE means not throw away
RemoveAt(0) Throw away the first element RemoveAt (listOfNames.Count-1) Throw away the last An element.
   If you provide an abnormal index, an error will be reported, such as a negative value or an index value that exceeds the length. The function does not return a value
  span>
   If you want to know the success or failure, you have to use try catch to check whether the Count has changed
RemoveAll() is the most complicated and powerful remove function. It uses a function’s delegate as a parameter. In this function, we can define our own remove logic and remove multiple items at once.
share picturethis The example logic is to remove the initial letter is J’s item

RomoveAll uses an anonymous method as a parameter. This anonymous method will be applied to each item. When applied, the parameter name is actually each item , To determine whether it is J begins and returns True False
< span style="font-size: 18px;">   judge whether to remove this item according to TRUE or False

< span style="font-size: 18px;">Sort() sorting function
Reverse() flip, head to tail, tail to head< /span>

List and List can be sorted, this is. How to sort the custom List that comes with Net?
  Use IComparable interface or use< code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid #eaeaea; background-color: #f8f8f8; border-radius: 3px; display: inline;">LINQ Or write a comparison function according to the nature of the item ( Sort can also accept a commission of a comparison function)
share picture

CompareUser here is a comparison function. I feel that the parameters of the delegate function of Remove above are similar.
Compare the size of the two numbers, the return value is smaller same bigger (-1 0 1) and then sort can sort up and down according to this value

?

List

List T is the element type in the list, the compiler It will check if the element type you add to the list is correct. If it is not, an error will be reported. In other words, the List is type safe.
List listOfStrings = new List();
List is very similar to Array(They both seem to pass< code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid #eaeaea; backgro und-color: #f8f8f8; border-radius: 3px; display: inline;">IList interface), but most In this case, list is easier to use than Arrays.
   For example, you don’t need to specify the number of elements, just add them. After adding, .Net will automatically help us correct the number, but note that the type of the added item must be the type represented by T< br>Share a picture

Initialization
share picture

The same is used curly braces and multiple elements are separated by commas

share picture

The above example is similar in structure to the Arrays example in the previous section. Pay attention to the use of List


Adding Adding element
Insert (index, item) Insert an element at the specified position, the original element and all following The elements are moved back by one place
AddRange(xxx) xxx can be any type of collection array or list (of course the data type of the element is still Same, string to string, int to int)
InsertRange(index,xxx)xxx refer to the above

Remove(“John”) Find John from the beginning, and throw him out of the list if you find it, you can’t find it No error is reported, the return value TRUE means throw away, FALSE means not thrown away
RemoveAt(0) Throw away the first element RemoveAt (listOfNames .Count-1) Throw away the last element.
   If you provide an abnormal index, an error will be reported, such as a negative value or an index value that exceeds the length. The function does not return a value
  span>
   If you want to know the success or failure, you have to use try catch to check whether the Count has changed
RemoveAll() is the most complicated and powerful remove function. It uses a function’s delegate as a parameter. In this function, we can define our own remove logic and remove multiple items at once.
share picturethis The example logic is to remove the initial letter is J’s item

RomoveAll uses an anonymous method as a parameter. This anonymous method will be applied to each item. When applied, the parameter name is actually each item , Determine whether it is J begins and returns True False
< span style="font-size: 18px;">   judge whether to remove this item according to TRUE or False

< span style="font-size: 18px;">Sort() sorting function
Reverse() flip, head to tail, tail to head< /span>

List and List can be sorted, this is. How to sort the custom List that comes with Net?
  Use IComparable interface or use< code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid #eaeaea; background-color: #f8f8f8; border-radius: 3px; display: inline;">LINQ Or write a comparison function according to the nature of the item ( Sort can also accept a commission of a comparison function)
share picture

CompareUser here is a comparison function. I feel that the parameters of the delegate function of Remove above are similar.
Compare the size of the two numbers, the return value is smaller same bigger (-1 0 1) and then sort can sort up and down according to this value

?

?< /span>

Leave a Comment

Your email address will not be published.