C # indexer, hash table Hashtabl, Dictionary

Source: Internet
Author: User
Indexer, hash table Hashtabl, Dictionary

 

I. Indexer

The indexer is similar to an attribute, but its get accessors use parameters. To declare the class or structure indexer, use the this keyword.

Example:

Sample Code of the Indexer

/// <Summary>
/// Class that stores the day of the week. Declares a get accessors. It accepts strings and returns corresponding integers.
/// </Summary>
Public class week
{
Public string [] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday "};

// The indexer accepts the string of the day of the week and uses it as the index.
Public int this [string day]
{
// Get accessors and return corresponding Integers
Get
{
Int I = 0;

Foreach (var item in weeks)
{
If (item = day)
{
Return I;
}
I ++;
}

// If no result is found,-1 is returned.
Return-1;
}
}
}

Ii. hashtable Hashtabl

Hastable is the implementation of hash tables. It can obtain key values based on keywords. The key type is object, and the value type is object.

Add a key/value pair in the hash table: HashtableObject. Add (key, value );

Remove a key/value pair in the hash table: HashtableObject. Remove (key );

Remove all elements from the hash table: HashtableObject. Clear ();

Determine whether the hash table Contains the specified key: HashtableObject. Contains (key );

You can use either of the following methods to traverse a Hashtable object:

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

Hashtable sample code

// Method 1
Foreach (System. Collections. DictionaryEntry de in myHashtable)
{
// Note that the default type stored in HastTable is object, which can be output only after conversion.
Console. WriteLine (de. Key. ToString ());
Console. WriteLine (de. Value. ToString ());
}

// Method 2
System. Collections. IDictionaryEnumerator enumerator = myHashtable. GetEnumerator ();

While (enumerator. MoveNext ())
{
Console. WriteLine (enumerator. Key); // Hashtable
Console. WriteLine (enumerator. Value); // Hashtable Value
}

 

3. Dictionary

Dictionary <Tkey, Tvalue> is a generic Implementation of Hastbale.

// Traverse Key
Foreach (string key in myDictionary. Keys)
{
// Traverse the value of a key
Foreach (string val in myDictionary [key])
{

}
}

 

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

Dictionary traversal example

Foreach (KeyValuePair <string, string> kvp in myDictionary)
{
String key = kvp. Key; // key contains the dictionary key
For (int I = 0; I <kvp. Value. Count; I ++)
{
Response. Write (kvp. Value [I]);
}
}

 

Example:

 

Code

// Define a <string, int> Dictionary to Add its value (you can also use the Add method)
Dictionary <string, int> dic = new Dictionary <string, int> ();

// Add two keys: "Score 1" and "score 2", and assign them 0.
Dic ["Score 1"] = 0;
Dic ["score 2"] = 0;

// Add the two values to 1
Dic ["Score 1"] ++;
Dic ["score 2"] ++;

 

 

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.