The collection operation discovery is getting richer and richer now. Today's Christmas, we take the commonly used list <t> as an example to quietly read some of the collection operations, which is really convenient to use.
1. merge, insert, delete, and display a set
Addrange (a set): adds the elements of a specified set to the end.
Removerange (the index starting from scratch for the element to be removed, and the number of elements to be removed): removes a certain range of elements.
Insertrange (insert the index of the new element from scratch, a set): insert the set to the specified index. You can also select a range for the set before inserting it.
Getrange (index starting from scratch at the beginning of the range, number of elements in the range): return data in the specified range. This data is a superficial copy, and the superficial copy only contains
Reference of elements in the Set
For example, the Code of program. CS:
[C-sharp]View plaincopy
- Using system;
- Using system. Collections. Generic;
- Using system. LINQ;
- Using system. text;
- Namespace consoleapplist
- {
- Class Program
- {
- Static void main (string [] ARGs)
- {
- String [] stuname = {"aaaa", "BBBB", "CCCC "};
- List <string> li = new list <string> (stuname );
- Console. writeline ("merge another set :");
- Li. addrange (LI );
- Foreach (string name in Li)
- {
- Console. writeline (name );
- }
- Console. writeline ("/R/n delete operation (specify the index location and number of deletions ):");
- Li. removerange (2, 2 );
- Foreach (string name in Li)
- {
- Console. writeline (name );
- }
- Console. writeline ("/R/n specify the index location and insert another set :");
- Stuname = new string [] {"Zhang San", "Li Si", "Wang Wu "};
- Li. insertrange (3, stuname );
- // Select the specified range of the SET TO BE INSERTED
- // Li. insertrange (3, new list <string> (stuname). getrange (1, 2 ));
- Foreach (string name in Li)
- {
- Console. writeline (name );
- }
- Console. writeline ("/R/N :");
- String [] Output = Li. getrange (2, 3). toarray ();
- // List <string> output = Li. getrange (2, 3 );
- Foreach (string name in output)
- {
- Console. writeline (name );
- }
- }
- }
- }
Running result:
Merge another set:
Aaaa
Bbbb
CCCC
Aaaa
Bbbb
CCCC
Delete operation (specify the index location and delete count ):
Aaaa
Bbbb
Bbbb
CCCC
Specify the index location and insert another set:
Aaaa
Bbbb
Bbbb
Zhang San
Li Si
Wang Wu
CCCC
Display data within the specified index range:
Bbbb
Zhang San
Li Si
Press any key to continue...
2. Set common judgment, selection, and computing functions
All () determines whether all elements in the sequence meet the conditions.
Any () determines whether any element in the sequence exists or meets the conditions.
Where () returns the elements that meet the conditions. predicate the filter value sequence.
Firstordefault () returns the first element in the sequence. If the sequence does not contain any element, the default value is returned.
Average value of the average () value sequence
Sum () value sequence sum
Distinct () returns non-repeating elements in the sequence
Union () generates the union of two sequences by using the default equal comparator. Remove duplicate elements
For example, program2.cs code:
[C-sharp]View plaincopy
- Using system;
- Using system. Collections. Generic;
- Using system. LINQ;
- Using system. text;
- Namespace consoleapplist
- {
- Public class student
- {
- Public string name {Get; set ;}
- Public String sex {Get; set ;}
- Public int age {Get; set ;}
- }
- Class Program
- {
- Static void main (string [] ARGs)
- {
- Student [] Stu = {New Student {name = "Zhang San", sex = "male", age = 22 },
- New student {name = "", sex = "male", age = 33 },
- New student {name = "Wang Wu", sex = "female", age = 19 }};
- List <student> li = new list <student> (Stu );
- // Whether all students are boys
- Bool allmale = Li. All (S => S. Sex = "male ");
- Console. writeline ("are all students boys?" + allmale );
- // Whether boys exist
- Bool anymale = Li. Any (S => S. Sex = "male ");
- Console. writeline ("/R/n students with boys:" + anymale );
- // Displays students older than or equal to 20
- Console. writeline ("/R/n shows students older than or equal to 20 :");
- Ienumerable <student> query = Li. Where (S => S. age> = 20 );
- Foreach (student s in query)
- {
- Console. writeline ("name:" + S. Name + ", Gender:" + S. Sex + ", age" + S. Age. tostring ());
- }
- // Returns the first element in the sequence.
- Console. writeline ("/R/n returns the first element in the sequence :");
- Student sfirstdef = Li. firstordefault <student> ();
- Console. writeline ("name:" + sfirstdef. Name + ", Gender:" + sfirstdef. Sex + ", age" + sfirstdef. Age. tostring ());
- // Average age of all students
- Double avgage = Li. Average <student> (S => S. Age );
- Console. writeline ("/R/n average age of all students:" + avgage. tostring ("F2 "));
- // Total age of all students
- Int avgsum = Li. Sum <student> (S => S. Age );
- Console. writeline ("/R/n total age of all students:" + avgsum );
- // Return non-repeating elements in the sequence
- List <int> ages = new list <int> };
- Ienumerable <int> distinctages = ages. Distinct <int> ();
- Console. writeline ("/R/n returned sequence of non-repeating elements :");
- Foreach (int A in distinctages)
- {
- Console. Write (a + ",");
- }
- Console. writeline ();
- // Generate the union of two sequences to remove duplicate elements
- Console. writeline ("/R/n generates the union of the two sequences, removing the repeated elements :");
- Int [] ints1 = {5, 3, 9, 7, 5 };
- Int [] ints2 = {8, 3, 6, 4, 4 };
- Ienumerable <int> Union = ints1.union (ints2 );
- Foreach (INT num in Union)
- Console. Write ("{0}", num );
- Console. writeline ();
- }
- }
- }
Running result:
Whether all students are boys: false
Students with boys: True
Show students older than or equal to 20:
Name: Zhang San, Gender: male, age 22
Name: Li Si, Gender: male, age 33
Returns the first element in the sequence:
Name: Zhang San, Gender: male, age 22
Average age of all students: 24.67
Total Age of all students: 74
Return non-repeating elements in the sequence:
, 33, 27,
Generate the union of the two sequences and remove the repeated elements:
5 3 9 7 8 6 4
Press any key to continue...
All, any, where, firstordefault, average, sum, distinct, union, addrange, removerange, insertrange, and getrange operations of the List