C# ハッシュテーブル

WBOY
リリース: 2024-09-03 15:04:59
オリジナル
431 人が閲覧しました

C# のハッシュテーブルは、キーと値のペアの形式のデータのコレクションであり、キーのハッシュ コードに基づいており、キーはコレクション内の要素またはデータにアクセスするために使用されます。これは、Objects クラスから Hashtable に継承されます。したがって、基本的に、C# またはその他のプログラミング言語のハッシュテーブルは、ハッシュ コード形式で適切に編成された、キーと値のペアの単純な表現です。

構文:

C# ハッシュテーブルとは何かを理解したので、ハッシュテーブルを適切に実装するための標準構文の理解に進みましょう。以下は、プログラムでハッシュテーブルを使用するための標準構文と必要なシステム ファイルです。

using System.Collections;
Hashtableht = new Hashtable();
ログイン後にコピー

コレクションを含むシステム ファイルは、ハッシュテーブルが使用する必要な関数とメソッドをインポートする役割を果たします。ここではハッシュテーブルが主要なキーワードであり、インスタンスを ht として作成し、操作は新しく作成された ht に対して実行されます。ハッシュテーブルを実装するための適切な構文がわかったので、それがどのように機能するかを理解しましょう。

Hashtable は C# でどのように機能しますか?

前に説明したように、ハッシュテーブルはキーと値のペアの形式のデータまたは情報のコレクションであることがわかっています。キーと値のペアの簡単な例は「名前: Sulaksh」です。ここでは、キーは名前、値は Sulaksh です。キーは同じままですが、値は異なる場合があります。ハッシュテーブルはキーと値で構成され、これらは中かっこで表され、計算にはハッシュ関数が使用されます。

次に、ハッシュテーブルの適切な実装に進み、例への取り組みを理解しましょう。

C# ハッシュテーブルの例

最初の例は、ハッシュテーブルの単純な実装です。数値のキーと値を含む単純なハッシュテーブルがあり、ハッシュテーブル内の要素の合計数を出力します。コードは次のとおりです。

例 #1

コード:

using System;
using System.Collections;
class sampleht {
static public void Main() {
Hashtableexampleht = new Hashtable();
exampleht.Add(1, " Element 1");
exampleht.Add(2, " Element 2");
exampleht.Add(3, " Element 3");
Console.WriteLine("\n The Total No. of elements: {0}", exampleht.Count);
}
}
ログイン後にコピー

コードの説明: システム ファイルから始まり、ここではコレクションが最も重要です。次にクラスがあり、その中にメイン メソッドがあります。 main メソッド内にはハッシュテーブルの宣言があり、その後に 3 つのキーと値のペアが続きます。要素を挿入するための add 関数を実装しました。したがって、ハッシュテーブルは 3 つのキーと値のペアで構成され、最後に、ハッシュテーブル内の要素の合計数である 3 を出力する print ステートメントがあります。ここでは単純なカウント関数を使用しています。出力については、以下に添付されたスクリーンショットを参照してください。

C# ハッシュテーブル

予想どおり、出力ではハッシュテーブルに 4 つの要素があることがわかります。次の例に進み、ハッシュテーブルのキーと値を表示してみます。

例 #2

コード:

using System;
using System.Collections;
class exampleHT {
static publicvoid Main() {
HashtablesampleHT = new Hashtable();
sampleHT.Add(1, " One");
sampleHT.Add(2, " Two");
sampleHT.Add(3, " Three");
Console.WriteLine("\n Below is the content of Hashtable: \n");
foreach (DictionaryEntry entry in sampleHT) {
Console.WriteLine(" {0}, {1}", entry.Key, entry.Value);
}
}
}
ログイン後にコピー

コードの説明: 前の例と同様に、メイン メソッドを含むシステム ファイルとクラスがあります。次に、ハッシュテーブル、キーと値のペア、print ステートメントが続きます。次に、一度に 1 つの要素を選択し、次の行でそれを出力として出力する foreach ステートメントがあります。出力はキーと値の形式の要素のリストであることが期待されます。以下のスクリーンショットを参照してください。

C# ハッシュテーブル

予想どおり、出力にはハッシュテーブルの要素が表示されます。次の例では、outhashtable を使用してクリア関数を実装します。コードは次のとおりです。

例 #3

コード:

using System;
using System.Collections;
class sampleht {
static public void Main()  {
Hashtableexampleht = new Hashtable();
exampleht.Add(1, " Element 1");
exampleht.Add(2, " Element 2");
exampleht.Add(3, " Element 3");
Console.WriteLine("\n Elements of the Hashtable :");
foreach(DictionaryEntry ele1 in exampleht) {
Console.WriteLine(ele1.Value);
}
Console.WriteLine(" No. of elements before clearing: {0} ", exampleht.Count);
exampleht.Clear();
Console.WriteLine(" Total No. of elements now: {0} ", exampleht.Count);
}
}
ログイン後にコピー

コードの説明: 必要なシステム ファイルとメソッドを使用して、ハッシュテーブルとその中で定義された合計 3 つのキーと値のペアができました。要素をハッシュテーブルに追加する add 関数を実装しました。次に、ハッシュテーブル内のすべての要素 (3 つ) を単純に出力する print ステートメントを実行します。ハッシュテーブルをクリアする前に、リストに存在する要素の総数を出力し、その後、ハッシュテーブル全体をクリアするclear関数を使用します。つまり、すべての要素がリストから削除され、最後のprintステートメントでその数が出力されます。現在存在する要素の数はゼロになります。

C# ハッシュテーブル

説明したように、clear 関数が機能し、リストがクリアされます。次に、次の例として、remove 関数を実装します。

例 #4

コード:

using System;
using System.Collections;
class sampleht {
static public void Main() {
Hashtableexampleht = new Hashtable();
exampleht.Add(1, " Element 1");
exampleht.Add(2, " Element 2");
exampleht.Add(3, " Element 3");
Console.WriteLine("\n List of Original Elements:");
foreach (var key in exampleht.Keys )
Console.WriteLine(" {0}, {1}",key , exampleht[key]);
exampleht.Remove(3);
exampleht.Remove(1);
Console.WriteLine("\n Elements after removal:");
foreach (var key in exampleht.Keys )
Console.WriteLine(" {0}, {1}",key , exampleht[key]);
}
}
ログイン後にコピー

Code Explanation: Just as our earlier examples, we have our system files, class, and the main method. Then we have our hashtable with a total of three key values. Then we have our print statement, which will print out the original list along with key and values, using foreach. We then have the remove function for two keys, which will remove the keys that we pass, then we have our print statement, which will use foreach and print every single element present in the hashtable after removing.

C# ハッシュテーブル

Advantages

Advantages of any function or methodology is important to understand its real time applications and hashtable’s most widely known advantage is how it allows the developer the ability for synchronization. Hashtable has noted advantages over the search tree and this makes them practical to use in real time computer applications and for the purpose of database indexing. Speed, compared to other table type data structures is best delivered by hashtables. Having key value pairs makes it easy to anticipate the format regarding data and restructuring is done easily.

Conclusion

Hashtable in C# is a collection of elements, represented in a key value pair format. The key can be the same, while values differ and the key cannot be null while a value can be. We implemented an example of functions like remove, clear, and print. Hashtable has a speed advantage over other data structures.

以上がC# ハッシュテーブルの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!