Indexer, hash table hashtabl, Dictionary dictionary (GO)

Source: Internet
Author: User

First, indexer

Indexers are similar to properties, except that their get accessors take parameters. To declare an indexer on a class or struct, use the This keyword.

Example:

Indexer Sample code///<summary>
The class that stores the day of the week. Declares a get accessor that accepts a string and returns the corresponding integer
</summary>
public class Week
{
Public string[] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Indexer it accepts a string of days of the week, it is indexed as index
public int this[string Day]
{
Get accessor and return the corresponding integer
Get
{
int i = 0;

foreach (var item in weeks)
{
if (item = = day)
{
return i;
}
i++;
}

Not found back-1
return-1;
}
}
}

Second, hash table hashtable

HasTable is the implementation of a hash table that can be based on key values, the type of the key is object, and the type of value is object.

Add a Key/value key value pair to the hash table: Hashtableobject.add (Key,value);

Remove a Key/value key value pair in the hash table: Hashtableobject.remove (key);

Remove all elements from the hash table: Hashtableobject.clear ();

Determines whether a hash table contains a specific key key:HashtableObject.Contains (key);

There are two ways to traverse a Hashtable object:

Because Hashtable each element is a key/value pair, the element type is neither the type of the key nor the type of the value, but the DictionaryEntry type.

Hashtable sample Code//Method one
foreach (System.Collections.DictionaryEntry de in myhashtable)
{
Note that the default type stored in Hasttable is object and needs to be converted before it can be output
Console.WriteLine (DE. Key.tostring ());
Console.WriteLine (DE. Value.tostring ());
}


Method Two
System.Collections.IDictionaryEnumerator enumerator = Myhashtable.getenumerator ();

while (enumerator. MoveNext ())
{
Console.WriteLine (Enumerator.       Key); Hashtable key word
Console.WriteLine (Enumerator.      Value); Hashtable value
}

Third, the Dictionary dictionary

Dictionary<tkey,tvalue> is a generic implementation of Hastbale.

Traverse key
foreach (string key in Mydictionary.keys)
{
Iterate over the value of a key
foreach (String val in Mydictionary[key])
{

}
}

Because Dictionary is a collection of keys and values, the element type is not a key type or a value type. Instead, the element type is the KeyValuePair of the key type and the value type.

Dictionary Traversal example foreach (keyvaluepair<string, string> kvp in MyDictionary)
{
String key = Kvp. Key;//key contains the keys in the dictionary.
for (int i = 0; i < KVP. Value.count; i++)
{
Response.Write (KVP. Value[i]);
}
}

Example:

Code

Define a <string,int> dictionary, and let its value be added (or you can use the Add method)
dictionary<string, int> dic = new dictionary<string, int> ();

Add two keys for "score 1", "Score 2", and assign them a value of 0
dic["Score 1"] = 0;
dic["Score 2"] = 0;

Add these two values to 1, respectively.
dic["score 1"]++;
dic["Score 2"]++;

Indexer, hash table hashtabl, Dictionary dictionary (GO)

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.