C # list <t> usage

Source: Internet
Author: User

C # list <t> usage

Namespace: system. Collections. Generic

Public class list <t>: ilist <t>, icollection <t>, ienumerable <t>, ilist, icollection, ienumerable

The list <t> class is the generic equivalent class of the arraylist class. This class implements the ilist <t> generic interface using an array that can be dynamically increased as needed.

 

The advantage of generics: it increases the efficiency and flexibility for compiling Object-oriented Programs in C # language. It does not forcibly pack or unbox the value type, or forcibly convert the reference type downward, so the performance is improved.

 

Performance considerations:

When deciding whether to use the ilist <t> or the arraylist class (both have similar functions), remember that the ilist <t> class performs better and is type-safe in most cases.

If you use the reference type for the type T of the ilist <t> class, the behavior of the two classes is identical. However, if you use value type T, you need to consider implementation and packing.

"Any reference or value type added to the arraylist will be implicitly forcibly converted to object. If the item is of the value type, you must perform the packing operation when adding it to the list, and cancel the packing operation during retrieval. Forced conversion, packing, and unboxing all reduce performance. When circular access is required for large sets, the effects of packing and unboxing are significant ."

 

1. Basic and common methods of list:

Statement:

1. List <t> MList = new list <t> ();

T is the element type in the list. The string type is used as an example.

E. g.: List <string> MList = new list <string> ();

 

2. List <t> testlist = new list <t> (ienumerable <t> collection );

Create a list using a set as the parameter

E. g .:

String [] temarr = {"ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "locu "};

List <string> testlist = new list <string> (temarr );

 

Add element:

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

E. g.: MList. Add ("John ");

2. Add a group of elements to list. addrange (ienumerable <t> collection ).

E. g .:

String [] temarr = {"ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "locu "};

MList. addrange (temarr );

3. insert (INT index, t item); add an element to the index position

E. g.: MList. insert (1, "Hei ");

 

Traverse the elements in the list:

The type of foreach (t element in MList) T is the same as that of MList declaration.

{

Console. writeline (element );

}

E. g .:

Foreach (string s in MList)

{

Console. writeline (s );

}

 

Delete element:

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

E. g.: MList. Remove ("Hunter ");

2. List. removeat (INT index); Delete the element whose subscript is index.

E. g.: MList. removeat (0 );

3. List. removerange (INT index, int count );

Delete count elements starting with subscript Index

E. g.: MList. removerange (3, 2 );

 

Determine whether an element is in the list:

List. Contains (T item) returns true or false, very useful

E. g .:

If (MList. Contains ("Hunter "))

{

Console. writeline ("there is Hunter in the List ");

}

Else

{

MList. Add ("Hunter ");

Console. writeline ("add hunter successfully .");

}

 

Sort the elements in the list:

List. Sort () is the first letter of the element in ascending order by default.

E. g.: MList. Sort ();

Reverse the order of elements in the list:

List. Reverse () can be used with list. Sort () to achieve the desired effect.

E. g.: MList. Sort ();

 

List clear: List. Clear ()

E. g.: MList. Clear ();

Obtain the number of elements in the list:

List. Count () returns the int value.

E. g .:

Int COUNT = MList. Count ();

Console. writeline ("the num of elements in the list:" + count );

 

2. Advanced and powerful methods of list:

Example list:

String [] temarr = {ha "," Hunter "," Tom "," Lily "," Jay "," Jim "," Kuku "," "locu "};

MList. addrange (temarr );

 

List. Find: searches for elements that match the conditions defined by the specified predicate, and returns the First Matching Element in the entire list.

Public t find (predicate <t> match );

Predicate is a method delegate. If the object passed to it matches the condition defined in the delegate, the method returns true. The elements of the current list are passed to the predicate delegate one by one and moved forward in the list, starting from the first element and ending with the last element. Processing stops when a match is found.

Predicate can be delegated to a function or a Lambda expression:

Delegate to lambda expressions:

E. g .:

String listfind = MList. Find (name => // name is a variable, representing the MList

{// Element, set by yourself

If (name. length> 3)

{

Return true;

}

Return false;

});

Console. writeline (listfind); // The output is Hunter.

 

Delegate to a function:

E. g .:

String listfind1 = MList. Find (listfind); // Delegate to the listfind Function

Console. writeline (listfind); // The output is Hunter.

 

Listfind function:

Public bool listfind (string name)

{

If (name. length> 3)

{

Return true;

}

Return false;

}

The results of the two methods are the same.

 

List. findlast method: searches for elements that match the conditions defined by the specified predicate and returns the last Matching Element in the entire list.

Public t findlast (predicate <t> match );

The usage is the same as that of list. Find.

 

List. trueforall method: determines whether each element in the list matches the condition defined by the specified predicate.

Public bool trueforall (predicate <t> match );

Delegate to lambda expressions:

E. g .:

Bool flag = MList. trueforall (name =>

{

If (name. length> 3)

{

Return true;

}

Else

{

Return false;

}

}

);

Console. writeline ("true for all:" + flag); // The flag value is false.

 

Delegate to a function. The above listfind function is used here:

E. g .:

Bool flag = MList. trueforall (listfind); // delegate it to the listfind Function

Console. writeline ("true for all:" + flag); // The flag value is false.

 

The results of the two methods are the same.

 

List. findall method: Retrieves all elements that match the conditions defined by the specified predicate.

Public list <t> findall (predicate <t> match );

E. g .:

List <string> sublist = MList. findall (listfind); // Delegate to the listfind Function

Foreach (string s in sublist)

{

Console. writeline ("element in sublist:" + S );

}

Sublist stores all elements with a length greater than 3.

 

List. Take (n): returns ienumetable <t> for the first n rows. The T type is the same as the list <t> type.

E. g .:

Ienumerable <string> takelist = MList. Take (5 );

Foreach (string s in takelist)

{

Console. writeline ("element in takelist:" + S );

}

In this case, takelist stores the first five elements in MList.

 

List. Where: Retrieves all elements that match the conditions defined by the specified predicate. Similar to the list. findall method.

E. g .:

Ienumerable <string> wherelist = MList. Where (name =>

{

If (name. length> 3)

{

Return true;

}

Else

{

Return false;

}

});

Foreach (string s in sublist)

{

Console. writeline ("element in sublist:" + S );

}

Sublist stores all elements with a length greater than 3.

 

List. removeall: removes all elements that match the conditions defined by the specified predicate.

Public int removeall (predicate <t> match );

E. g .:

MList. removeall (name =>

{

If (name. length> 3)

{

Return true;

}

Else

{

Return false;

}

});

Foreach (string s in MList)

{

Console. writeline ("element in MList:" + S );

}

In this case, the MList stores the elements with a length greater than 3.

 

 

 

 

List <t> is a generic linked list... t represents the node element type.
For example
List <int> intlist; indicates a linked list whose element is int.
Intlist. Add (34); // Add
Intlist. Remove (34); // Delete
Intlist. removeat (0); // deletes an element located somewhere.
Intlist. Count; // The length of the linked list.
There are also insert, find, findall, contains, and other methods, as well as the index method intlist [0] = 23;
1. Reduced packing and unpacking
2. Check the data type during compilation.

List <Object> is equivalent to the list in the system. Collections namespace.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.