Home > Backend Development > C#.Net Tutorial > Introduction to C# List usage details

Introduction to C# List usage details

高洛峰
Release: 2016-12-15 15:34:17
Original
2746 people have browsed it

1 About the introduction

(1) Namespace: System.Collections.Generic

public class List : IList, ICollection, IEnumerable, IList, ICollection, IEnumerable.

(2) The List class is the generic equivalent class of the ArrayList class. This class implements the IList generic interface using an array whose size can be dynamically increased as needed.

(3) Benefits of generics: It adds great efficiency and flexibility to writing object-oriented programs using the C# language. There is no forced boxing and unboxing of value types, or downcasting of reference types, so performance is improved.

(4) Performance Note: When deciding to use List or ArrayList class (both have similar functionality), remember that List class performs better in most cases and is type-safe of.

If you use a reference type for the type T of the List class, the behavior of the two classes is exactly the same. However, if you use a value type for type T, you need to consider implementation and boxing issues.

(5) In Microsoft’s words:

“Any reference or value type added to the ArrayList will be implicitly upcast to Object. If the item is a value type, it must be cast when adding it to the list. Boxing, unboxing while retrieving, and boxing and unboxing all reduce performance; the impact of boxing and unboxing is significant when you have to iterate over a large collection. ."

Common methods

1 Statement:

(1)List mList = new List();

T is the type of elements in the list. Now take the string type as an example

For example: List< ;string> mList = new List();

(2)List testList =new List (IEnumerable collection);

Create a List with a collection as a parameter

string[ ] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };

List testList = new List (temArr);

2 Add element:

(1) List. Add(T item) Add an element

mList.Add("John");

(2) List. AddRange(IEnumerable collection ) Add a set of elements

string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };

mList. AddRange(temArr);

(3)Insert(int index, T item); Add an element at the index position

mList.Insert(1, "Hei");

Traverse the elements in the List:

foreach (T element in mList) The type of T is the same as when mList was declared

{

Console.WriteLine(element);

} as follows:

foreach (string s in mList)

{

Console.WriteLine(s);

}

2 Delete element

(1)List.Remove(T item) Delete a value

such as: mList.Remove("Hunter");

(2) List.RemoveAt(int index); Delete The element whose subscript is index

such as mList.RemoveAt(0);

(3) List. RemoveRange(int index, int count);

Start from the subscript index and delete count elements

such as mList.RemoveRange(3, 2);

3 Determine whether an element is in the List Medium:

List. Contains(T item) Returns true or false, very practical

if (mList.Contains("Hunter"))

{

Console.WriteLine("There is Hunter in the list");

}

else

{

mList.Add("Hunter");

Console.WriteLine("Add Hunter successfully.");

}



More introduction to C# List& lt ;T>For detailed usage information, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template