C#List<T>Generic List

List<T>

I. Introduction

This class implements IList, ICollection, IEnumerable, IList, ICollection, IEnumerable interfaces.

We also mentioned List generic collection classes briefly in the previous section.Now let's review.

First, the array stores fixed data types and lengths.Clearly, this feature is not sufficient for storage needs of different lengths.Solid introduces the concept of collections, where List s are generic collections and can be oriented to any data type.

2. Create lists

Creating lists can be expressed in a variety of ways, either defining a length or not.You can assign values directly when you define them, or you can add them later with Add().

//Create List Method 1:
var myList=new List<int>();  //Create a list of type int, no length specified
myList.Capacity=10;    //You can also set capacity size by attributes

//Create List Method 2:
List<int> myList2=new List<int>();  //Create a list of type int, no length specified
myList2.Capacity=10;    //You can also set capacity size by attributes
myList2.Add(9);         //Add Elements
myList2.Add(8);         //Add Elements

//Create List Method 3:
var myList3=new List<int>(10);  //Create a list of type int with a base length of 10, double to 20 if not enough, and double to 40 if not enough.And so on.

//Create List Method 4:
var myList4=new List<int>(){1,2,3};   //Definition is assignment
//Int[] myArray = new int[] {1,2,3,4,5}; //Common Array Definition
Console.WriteLine("myList4 The number of elements stored in the collection is:"+myList4.Count); //Output is 3

//Create List Method 5:
var myList5=new List<string>("one","two");

From the example above, we know about how lists are created.The following points to note are available:
1. If the list size is not specified or not large enough, it will automatically double according to the number of data stored, that is, 1, 2, 4, 8, 16.And so on.
2. When the contents of the list exceed the size, the internal principle is to copy the existing T-type array into a doubled new array.Therefore, doubling capacity will bring some efficiency problems.
3. If the implementation knows the size of the data it needs to store, it can specify the size of the list.Performance loss due to capacity doubling.
4. If you don't know the size of the data you need to store, set the capacity as large as possible.This reduces the number of capacity doubles and also improves efficiency.
5. The number of existing data in the collection that can be read through Count's properties.
6. If you have already added all the elements to the list and don't want to add the elements with grace, you can call the TrimExcess() method to remove unwanted capacity.
7. You can assign values conveniently in the creation of a collection.Pixel groups are the same.

3. Adding Elements

As already mentioned in the example in 2, how to add elements.
In general, there are several ways to add elements to a list:

var myList = new List<int>() { 0,0,0};  //Create a list of type int, no length specified
myList.Capacity=10;               //You can also set capacity size by attributes

//Add Element Method 1:
List<int> myList2 = new List<int>() { 1,2,3};//Create a list of type int, no length specified, and add elements 1, 2, 3
myList2.Capacity=10;              //You can also set capacity size by attributes

//Add Element Method 2:
myList2.Add(9);                   //Add Elements
myList2.Add(8);                   //Add Elements

//Add Element Method 3:
myList2.AddRange(myList);         //Add an existing collection to the end

//Add Element Method 4:
myList2.Insert(1,33);             //Insert from specified index

//Add Element Method 5:
myList2.InsertRange(4,myList);    //Inserts a collection from a specified index


foreach (int i in myList2)
{
    Console.WriteLine(i);         //Traverse Set
}

4. Delete Elements

There are also several ways to delete elements:

//Method 1:
myList2.Remove(0);          //Delete the first item with a value of 0

//Method 2:
myList2.RemoveAt(0);        //Delete items with index 0

//Method 3:
myList2.RemoveRange(2,2);   //Delete elements of length 2 starting at index 2

//Method 4:
myList2.Clear();            //Delete all elements

5. Search Elements

Lists can also search for elements in several ways:

//Create List Method 1:
var myList = new List<int>() {1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4};  //Create a list of type int, no length specified

//Mode 1:
myList.IndexOf(1,0,15);   //Search 1's first occurrence of location index in the range 0-15

//Method 2:
myList.LastIndexOf(3);    //Search 3 Last Location Index

//Method 3:
myList.FindIndex();

//Method 4:
myList.FindLastIndex();

//Method 5:
myList.Find();

//Method 6:
myList.FindLast();

Methods 3 through 6 above refer to a parameter Predicate match.
Where Predicata is a delegate that returns a Boolean value and requires T as a parameter.If the Predicata delegate returns true, a matching element is found.

The Predicate delegate is defined as follows:

public delegate bool Predicate (T obj);

37 original articles published, 0 praised, 646 visits
Private letter follow

Added by mfalomir on Mon, 03 Feb 2020 04:47:57 +0200