The namespace system. Collections. Generic has two very important and common generic collection classes, which are dictionary <tkey, tvalue> dictionary and list <t> list.The dictionary is usually used to save the data of key/value pairs, while the List list is used to save the list of strong types of objects that can be accessed through the index.The following is a summary.CodeTo demonstrate how to initialize, add, modify, delete, and traverse elements.
Dictionary <tkey, tvalue> dictionary
The Code is as follows:
Namespace Dictionarydemo1
{
Class Program
{
Static Void Main ( String [] ARGs)
{
// Create a dictionary <tkey, tvalue> Object
Dictionary < String , String > Openwith = New Dictionary < String , String > ();
// There are two methods to add elements to an object. Note: The keys in the dictionary cannot be repeated, but the values can be repeated.
// Method 1: Use the add () method of the object
Openwith. Add ( " Txt " , " Notepad.exe " );
Openwith. Add ( " BMP " , " Paint.exe " );
Openwith. Add ( " DiB " , " Paint.exe " );
Openwith. Add ( " RTF " , " Wordpad.exe " );
// Method 2: Use Indexer
// Openwith ["TXT"] = "notepad.exe ";
// Openwith ["BMP"] = "paint.exe ";
// Openwith ["Dib"] = "paint.exe ";
// Openwith ["rtf"] = "wordpad.exe ";
// Add an element. Before adding a key, check whether the containskey () method is used.
If (! Openwith. containskey ( " HT " ))
{
Openwith. Add (" HT " , " Hypertrm.exe " ); // Or openwith ["ht"] = "hypertrm.exe ";
Console. writeline ( " Element Added! Key = {0}, value = {1} " , " HT " , Openwith [" HT " ]);
}
// Delete an element and use the remove () method.
If (Openwith. containskey ( " RTF " ))
{
Openwith. Remove ( " RTF " );
Console. writeline ( " Element deleted! The key is RTF. " );
}
If (! Openwith. containskey ( " RTF " ))
{
Console. writeline ( " The key = \ "RTF \" element cannot be found! " );
}
// Modify the element and use the indexer.
If (Openwith. containskey ( " Txt " ))
{
Openwith [ " Txt " ] = " Notepadupdate.exe " ;
Console. writeline (" Element modified! Key = {0}, value = {1} " , " Txt " , Openwith [ " Txt " ]);
}
// This class implements the ienumerable interface, so you can use the foreach statement. Note that the element type is keyvaluepair (of tkey, tvalue)
Foreach (Keyvaluepair <String , String > KVp In Openwith)
{
Console. writeline ( " Key = {0}, value = {1} " , KVp. Key, kVp. value );
}
Console. writeline ( " Element traversal is completed! " );
Console. readkey ();
}
}
}
ProgramOutput result:
List <t> List
The Code is as follows:
Namespace Listdemo1
{
Class Program
{
Static Void Main ( String [] ARGs)
{
// Create a list <t> list object
List <String > Din1_urs = New List < String > ();
// Add an element to the list (or initialization). Note that the indexer cannot be used during initialization because the list is empty before adding an element.
Dinosaurs. Add ( " Tyrannosaurus " );
Dinosaurs. Add ( " Amargasaurus " );
Dinosaurs. Add ( " Mamenchisaurus " );
Dinosaurs. Add ( " Deinonychus " );
Dinosaurs. Add ( " Compsognathus " );
// An important attribute
Console. writeline (" Number of elements in the list: {0} " , Din1_urs. Count ); // Obtains the number of actually contained elements in the list.
// Insert element, using the insert () method
Din1_urs. insert ( 2 , " Compsognathus " ); // Insert the element to the specified index.
Console. writeline ( " Insert element {0} At index 2} " , Din1_urs [ 2 ]);
// Delete an element and use the remove () method.
Dinosaurs. Remove ( " Compsognathus " ); // Removes the first matching item of a specific object from the list.
Console. writeline (" Delete the first element named compsognathus! " );
// Modify the element and use the indexer.
Din1_urs [ 0 ] = " Tyrannosaurusupdate " ;
Console. writeline ( " Successfully modified the element whose index is 0! " );
// Traverse elements using the foreach statement. The element type is string.
Foreach ( String Dinosaur In Dinosaurs)
{
Console. writeline (dinw.ur );
}
Console. writeline ( " Element traversal is completed! " );
Console. readkey ();
}
}
}
Program output result: