Lan Unity Development Fundamentals Two--lesson 24 Dictionary
one, Dictionary
Dictionary is a collection of stored keys and values
Dictionary are unordered, key keys are unique
Recommended Video lecturer Blog: http://11165165.blog.51cto.com/
usingSystem;
// Reference generic collection namespace
usingSystem.Collections.Generic;
namespacelesson_24
{
class MainClass
{
public Static voidMain (string[] Args)
{
// Create a dictionary object, The type of key is string, The type of Value is int
Dictionary<string,int> Dic=New Dictionary<string,int> ();
The//add method is used to add a Key-value pair
Dic. ADD ("laowang", -);
Dic. ADD ("laozhang", -);
// Remove key-value pairs from the dictionary
Dic. Remove ("laowang");
// Empty current dictionary
Dic. Clear ();
//gets The number of KeyValue in the current dictionary
intcount = Dic. Count;
Console. WriteLine (" There are in the current dictionary " +count+" a keyvalue." );
// Check if the dictionary contains the specified Key
BOOLB=dic. ContainsKey ("xiaoming");
// Check whether the dictionary contains the specified Value
BOOLC = Dic. Containsvalue ( the);
// try to get the Value corresponding to the specified key
ints;
Dic. TryGetValue ("xiaoming", outs);
// If the current dictionary contains xiaoming This key, then the corresponding Value is obtained and saved in the s in bb=true
// If the current dictionary does not contain xiaoming This key, then s=null,bb= False
// get Value by Key
intage= dic["laowang"];
Console. WriteLine (age);
}
}
}
Lan Unity Development Fundamentals Two--lesson 24 Dictionary