Thread-Safe Data Dictionary Dictionary

Source: Internet
Author: User
Using System;
Using System.Collections;
Using System.Collections.Generic;
Using System.Diagnostics;
Using System.Reflection;
Using System.Runtime;
Using System.Runtime.InteropServices;
Using System.Runtime.Serialization;
Using System.Security;
Using System.Threading;


Namespace Dictionary
{
<summary>
Thread-Safe Data dictionary
</summary>
<typeparam name= "TKey" ></typeparam>
<typeparam name= "TValue" ></typeparam>
public class Synchronizeddictionary<tkey, tvalue>
{
Private Dictionary<tkey, tvalue> dic=new dictionary<tkey,tvalue> ();
Private Object obj = new Object ();


<summary>
Add a key-value pair to the dictionary
</summary>
<param name= "Key" ></param>
<param name= "Value" ></param>
public void Add (TKey key, TValue value)
{
Lock (obj)
{
if (ContainsKey (key))
{
Remove (key);
}
Dic. ADD (key, value);
}
}


<summary>
Add a list to the dictionary
</summary>
<param name= "ValueList" ></param>
<param name= "func" ></param>
public void AddRange (icollection<tvalue> valueList, func<tvalue,tkey> Func)
{
Lock (obj)
{
if (func! = null)
{
TKey key = Default (TKey);


foreach (var item in valueList)
{
Key = func (item);

if (ContainsKey (key))
{
Dic. Remove (key);
}


Dic. ADD (key, item);


Key = Default (TKey);
}
}
}
}


<summary>
Empty data dictionary
</summary>
public void Clear ()
{
Lock (obj)
{
Dic. Clear ();
}
}


<summary>
Remove a specific key-value pair from the dictionary
</summary>
<param name= "Key" ></param>
<returns></returns>
public bool Remove (TKey key)
{
Lock (obj)
{
return DIC. Remove (key);
}
}


<summary>
Whether the dictionary contains a specific key
</summary>
<param name= "Key" ></param>
<returns></returns>
public bool ContainsKey (TKey key)
{
Lock (obj)
{
return DIC. ContainsKey (key);
}
}

<summary>
Whether the dictionary contains a specific value
</summary>
<param name= "Value" ></param>
<returns></returns>
public bool Containsvalue (TValue value)
{
Lock (obj)
{
return DIC. Containsvalue (value);
}
}

<summary>
Number of key-value pairs in the dictionary
</summary>
public int Count
{
Get
{
Lock (obj)
{
return DIC. Count;
}
}
}


<summary>
Key collection of dictionaries
</summary>
Public Dictionary<tkey, Tvalue>. KeyCollection Keys
{
Get
{
Lock (obj)
{
return DIC. Keys;
}
}
}


<summary>
Dictionary Value Collection
</summary>
Public Dictionary<tkey, Tvalue>. ValueCollection Values
{
Get
{
Lock (obj)
{
return DIC. Values;
}
}
}


<summary>
Dictionary key Array
</summary>
Public tkey[] Keystoarray
{
Get
{
Lock (obj)
{
Tkey[] keys = new tkey[dic. Keys.count];
Dic. Keys.copyto (keys, 0);
return keys;
}
}
}


<summary>
Array of dictionary values
</summary>
Public tvalue[] Valuestoarray
{
Get
{
Lock (obj)
{
Tvalue[] values = new tvalue[dic. Keys.count];
Dic. Values.copyto (values, 0);
return values;
}
}
}

<summary>
Returns an enumeration of the dictionary iterates
</summary>
<returns></returns>
Public Dictionary<tkey, Tvalue>. Enumerator GetEnumerator ()
{
Lock (obj)
{
return DIC. GetEnumerator ();
}
}

<summary>
///
</summary>
Public iequalitycomparer<tkey> Comparer
{
Get
{
Lock (obj)
{
return dic.comparer;
}
}
}


<summary>
Get a specific value by key
</summary>
<param name= "Key" ></param>
<returns></returns>
Public TValue This[tkey Key]
{


Get
{
Lock (obj)
{
return Dic[key];
}
}
Set
{
Lock (obj)
{
Dic[key] = value;
}
}
}


<summary>
Gets the value of a dictionary-specific location
</summary>
<param name= "Index" ></param>
<returns></returns>
Public TValue Value (int index)
{


Lock (obj)
{
return Valuestoarray[index];
}
}


<summary>
Get keys for a specific location in a dictionary
</summary>
<param name= "Index" ></param>
<returns></returns>
Public TKey Key (int index)
{


Lock (obj)
{
return Keystoarray[index];
}
}


}
}

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.