Chia sẻ

Nội dung

Dictionary Properties

The Dictionary class has three properties – Count, Keys and Values.

Count

The Count property gets the number of key/value pairs in a Dictionary. The following code snippet display number of items in a dictionary

Console.WriteLine("Count: {0}", AuthorList.Count);

Item

The Item property gets and sets the value associated with the specified key. The following code snippet sets and gets an items value.

// Set Item value
AuthorList["Mahesh Chand"] = 20;
// Get Item value
Int16 age = Convert.ToInt16(AuthorList["Mahesh Chand"]);

Keys

The Keys property gets a collection containing the keys in the Dictionary. It returns an object of KeyCollection type. The following code snippet reads all keys in a Dictionary.

// Get and display keys
Dictionary<string, Int16>.KeyCollection keys = AuthorList.Keys;
foreach (string key in keys)
{
    Console.WriteLine("Key: {0}", key);
}

Values

The Values property gets a collection containing the values in the Dictionary. It returns an object of ValueCollection type. The following code snippet reads all values in a Dictionary.

// Get and display values
Dictionary<string, Int16>.ValueCollection values = AuthorList.Values;
foreach (Int16 val in values)
{
    Console.WriteLine("Value: {0}", val);
}

Dictionary Methods

The Dictionary class is a generic collection and provides all common methods to add, remove, find and
replace items in the collection.

Add Items

The Add method adds an item to the Dictionary collection in form of a key and a value. The following code snippet creates a Dictionary and adds an item to it by using the Add method.

Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();
AuthorList.Add("Mahesh Chand", 35);

Alternatively, we can use the Item property. If the key does not exist in the collection, a new item is added. If the same key already exists in the collection, the item value is updated to the new value. The following code snippet adds an item and updates the existing item in the collection.

AuthorList["Neel Beniwal"] = 9;
AuthorList["Mahesh Chand"] = 20;

Remove Item

The Remove method removes an item with the specified key from the collection. The following code snippet removes an item.

// Remove item with key = 'Mahesh Chand'
AuthorList.Remove("Mahesh Chand");

The Clear method removes all items from the collection. The following code snippet removes all items by calling the Clear method.

// Remove all items
AuthorList.Clear();

Find a Key

The ContainsKey method checks if a key is already exists in the dictionary. The following code snippet checks if a key is already exits and if not, add one.

if (!AuthorList.ContainsKey("Mahesh Chand"))
{
    AuthorList["Mahesh Chand"] = 20;
}

Find a Value

The ContainsValue method checks if a value is already exists in the dictionary. The following code snippet checks if a value is already exits.

if (!AuthorList.ContainsValue(9))
{ 
    Console.WriteLine("Item found");
}

Sample

Here is the complete sample code showing how to use these methods.

// Create a dictionary with string key and Int16 value pair
Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();
AuthorList.Add("Mahesh Chand", 35);
AuthorList.Add("Mike Gold", 25);
AuthorList.Add("Praveen Kumar", 29);
AuthorList.Add("Raj Beniwal", 21);
AuthorList.Add("Dinesh Beniwal", 84);
// Count
Console.WriteLine("Count: {0}", AuthorList.Count);
// Set Item value
AuthorList["Neel Beniwal"] = 9;
if (!AuthorList.ContainsKey("Mahesh Chand"))
{
    AuthorList["Mahesh Chand"] = 20;
}
if (!AuthorList.ContainsValue(9))
{
    Console.WriteLine("Item found");
}
// Read all items
Console.WriteLine("Authors all items:");
foreach (KeyValuePair<string, Int16> author in AuthorList)
{
    Console.WriteLine("Key: {0}, Value: {1}",author.Key, author.Value);
}
// Remove item with key = 'Mahesh Chand'
AuthorList.Remove("Mahesh Chand");
// Remove all items
AuthorList.Clear();

Chia sẻ

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *