Hashtable . . - . .NET Framework. , .
Hashtable . , Hashtable . , [].
:
Hashtable.
Hashtable [#]
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
}
}
13, Thirteen
2, Two
1, One
DictionaryEntry, foreach. WriteLine , / .
Foreach
Hashtables, DictionaryEntry foreach-loop. ArrayList. A DictionaryEntry : .
Foreach
DictionaryEntry
Hashtable. ContainsKey Hashtable . true, .
. [].
, Contains [#]
using System;
using System.Collections;
class Program
{
static Hashtable GetHashtable()
{
Hashtable hashtable = new Hashtable();
hashtable.Add("Area", 1000);
hashtable.Add("Perimeter", 55);
hashtable.Add("Mortgage", 540);
return hashtable;
}
static void Main()
{
Hashtable hashtable = GetHashtable();
Console.WriteLine(hashtable.ContainsKey("Perimeter"));
Console.WriteLine(hashtable.Contains("Area"));
int value = (int)hashtable["Area"];
Console.WriteLine(value);
}
}
True
True
1000
- , . Hashtable . , . . .
Hashtable. int. / . Hashtable.
, [#]
using System;
using System.Collections;
class Program
{
static Hashtable GetHashtable()
{
Hashtable hashtable = new Hashtable();
hashtable.Add(300, "Carrot");
hashtable.Add("Area", 1000);
return hashtable;
}
static void Main()
{
Hashtable hashtable = GetHashtable();
string value1 = (string)hashtable[300];
Console.WriteLine(value1);
int value2 = (int)hashtable["Area"];
Console.WriteLine(value2);
}
}
Carrot
1000
. - . , InvalidCastException. , is as.
Hashtable. as-operator, . , .
Null
:
is-operator. true false .
, Hashtable [#]
using System;
using System.Collections;
using System.IO;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable.Add(400, "Blazer");
string value = hashtable[400] as string;
if (value != null)
{
Console.WriteLine(value);
}
StreamReader reader = hashtable[400] as StreamReader;
if (reader != null)
{
Console.WriteLine("Unexpected");
}
object value2 = hashtable[400];
if (value2 is string)
{
Console.Write("is string: ");
Console.WriteLine(value2);
}
}
}
Blazer
: Blazer
Hashtable as-operator. , FxCop, Microsoft.
FxCop
* , *
. ArrayList. , , ArrayList.
:
# Hashtable Keys. .
, ArrayList [#]
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable.Add(400, "Blaze");
hashtable.Add(500, "Fiery");
hashtable.Add(600, "Fire");
hashtable.Add(800, "Immolate");
foreach (int key in hashtable.Keys)
{
Console.WriteLine(key);
}
foreach (string value in hashtable.Values)
{
Console.WriteLine(value);
}
ArrayList arrayList = new ArrayList(hashtable.Keys);
foreach (int key in arrayList)
{
Console.WriteLine(key);
}
}
}
800 (First loop)
600
500
400
Immolate (Second loop)
Fire
Fiery
Blaze
800 (Third loop)
600
500
400
. , Keys Hashtable. foreach .
. , Hashtable. , Hashtable, .
Console.WriteLine
ArrayList. ArrayList Keys ( Values). ArrayList ArrayList.
ArrayList
:
. accessers Hashtable .
:
, Hashtable.
Count, Clear
Hashtable Count. Clear Hashtable. Hashtable Hashtable().
:
, Count. .
, Count Hashtable [#]
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable.Add(1, "Sandy");
hashtable.Add(2, "Bruce");
hashtable.Add(3, "Fourth");
hashtable.Add(10, "July");
int count = hashtable.Count;
Console.WriteLine(count);
hashtable.Clear();
Console.WriteLine(hashtable.Count);
}
}
4
0
-, Hashtable. , 4. Clear Hashtable, 0 . Hashtable , null.
Count Hashtable. Hashtable , Hashtable. Count .
:
MSDN , Count " O (1)".
. . , Hashtable ( ). .
Benchmark
, Hashtable System.Collections Dictionary System.Collections.Generic. .
:
, . 20 .
Hashtable, [#]
Hashtable hashtable = new Hashtable();
for (int i = 0; i < 10000; i++)
{
hashtable[i.ToString("00000")] = i;
}
Dictionary used in benchmark [C#]
var dictionary = new Dictionary<string, int>();
for (int i = 0; i < 10000; i++)
{
dictionary.Add(i.ToString("00000"), i);
}
Statements benchmarked [C#]
hashtable.ContainsKey("09999")
hashtable.ContainsKey("30000")
dictionary.ContainsKey("09999")
dictionary.ContainsKey("30000")
20
Hashtable result: 966 ms
Dictionary result: 673 ms
Hashtable , . , Hashtable 30% . , .
Hashtable 15 . . Hashtable. , -.
Designer Tips Summary
We used the Hashtable collection. This is an older collection, obsolete from the Dictionary collection. Knowing how to use it is crucial when maintaining old programs. These programs are important for many organizations.