logo

例を含む C# 辞書

C# では、Dictionary は一般にキーと値のペアを格納するために使用される汎用コレクションです。 Dictionary の動作は、非汎用 hashtable と非常によく似ています。 Dictionary の利点は、ジェネリック型であることです。ディクショナリは System.Collections.Generic 名前空間で定義されます。本質的に動的であるということは、辞書のサイズが必要に応じて増加することを意味します。 重要な点:

  • Dictionary クラスは、
    • I辞書 インターフェース
    • IReadOnlyコレクション インターフェース
    • IReadOnlyDictionary インターフェース
    • I辞書 インターフェース
  • Dictionary では、キーを null にすることはできませんが、値を null にすることはできます。
  • Dictionary では、キーは一意である必要があります。重複キーは許可されていません。重複キーを使用しようとすると、コンパイラが例外をスローします。
  • Dictionary には、同じ種類の要素のみを保存できます。
  • Dictionary の容量は、Dictionary が保持できる要素の数です。

辞書を作成するにはどうすればよいですか?

Dictionaryクラスには 7 つのコンストラクター これらは辞書を作成するために使用されます。ここでは Dictionary() コンストラクターのみを使用します。コンストラクターについて詳しく知りたい場合は、を参照してください。 C# |辞書クラス辞書(): このコンストラクターは、次のように空で、デフォルトの初期容量を持ち、キー タイプのデフォルトの等価比較子を使用する Dictionary クラスのインスタンスを作成するために使用されます。 ステップ1: キーワードを使用して、System.Collections.Generic 名前空間をプログラムに含めます。 構文:



using System.Collections.Generic;>

ステップ2: 以下に示すように、Dictionary クラスを使用して辞書を作成します。

Dictionary dictionary_name = new Dictionary();>

ステップ 3: ディクショナリに要素を追加する場合は、Add() メソッドを使用してキーと値のペアをディクショナリに追加します。また、Add メソッドを使用せずに、キーと値のペアを辞書に追加することもできます。以下の例に示すように。 ステップ 4: ディクショナリのキーと値のペアには、次を使用してアクセスします。 三つ 違う方法:

    for ループ: for ループを使用して、ディクショナリのキーと値のペアにアクセスできます。 例:

Cシャープ








for>(>int> x=0; x { Console.WriteLine('{0} and {1}', My_dict1.Keys.ElementAt(x), My_dict1[ My_dict1.Keys.ElementAt(x)]); }>

>

>

    インデックスの使用: インデックス値を使用して、ディクショナリの個々のキーと値のペアにアクセスできます。ここでは、インデックスにキーを指定するだけで、指定された辞書から値を取得できます。インデックスを指定する必要はありません。インデクサーは常にキーをパラメーターとして受け取ります。指定されたキーが辞書で使用できない場合は、次の値を返します。 KeyNotFoundException例:

Cシャープ




Console.WriteLine('Value>is>:{0}', My_dicti[1123]);> Console.WriteLine('Value>is>:{0}', My_dicti[1125]);>

>

>

    foreach ループ: foreach ループを使用して、ディクショナリのキーと値のペアにアクセスできます。以下の例に示すように、foreach ループを使用してディクショナリにアクセスします。

例:

Cシャープ




// C# program to illustrate how> // to create a dictionary> using> System;> using> System.Collections.Generic;> class> GFG {> >// Main Method> >static> public> void> Main () {> > >// Creating a dictionary> >// using Dictionary class> >Dictionary<>int>,>string>>My_dict1 =>> new> Dictionary<>int>,>string>>();>> > >// Adding key/value pairs> >// in the Dictionary> >// Using Add() method> >My_dict1.Add(1123, 'Welcome');> >My_dict1.Add(1124, 'to');> >My_dict1.Add(1125, 'techcodeview.com');> > >foreach>(KeyValuePair<>int>,>string>>エレ1>>My_dict1)> >{> >Console.WriteLine('{0} and {1}',> >ele1.Key, ele1.Value);> >}> >Console.WriteLine();> > >// Creating another dictionary> >// using Dictionary class> >// adding key/value pairs without> >// using Add method> >Dictionary<>string>,>string>>My_dict2 =>> >new> Dictionary<>string>,>string>>(){>> >{'a.1', 'Dog'},> >{'a.2', 'Cat'},> >{'a.3', 'Pig'} };> > >foreach>(KeyValuePair<>string>,>string>>エレ2>>in> My_dict2)> >{> >Console.WriteLine('{0} and {1}', ele2.Key, ele2.Value);> >}> >}> }>

>

>

出力:

1123 and Welcome 1124 and to 1125 and techcodeview.com a.1 and Dog a.2 and Cat a.3 and Pig>

辞書から要素を削除するにはどうすればよいですか?

Dictionary では、Dictionary から要素を削除できます。 Dictionary クラスは、要素を削除する 2 つの異なるメソッドを提供します。メソッドは次のとおりです。

  • クリア : このメソッドは、辞書からすべてのキーと値を削除します。
  • 取り除く : このメソッドは、指定されたキーを持つ値を辞書から削除します。

例:

Cシャープ




// C# program to illustrate how> // remove key/value pairs from> // the dictionary> using> System;> using> System.Collections.Generic;> class> GFG {> >// Main Method> >static> public> void> Main() {> > >// Creating a dictionary> >// using Dictionary class> >Dictionary<>int>,>string>>My_dict =>> >new> Dictionary<>int>,>string>>();>> > >// Adding key/value pairs in the> >// Dictionary Using Add() method> >My_dict.Add(1123, 'Welcome');> >My_dict.Add(1124, 'to');> >My_dict.Add(1125, 'techcodeview.com');> > >// Before Remove() method> >foreach>(KeyValuePair<>int>,>string>>彼は>>My_dict)> >{> >Console.WriteLine('{0} and {1}',> >ele.Key, ele.Value);> >}> >Console.WriteLine();> > >// Using Remove() method> >My_dict.Remove(1123);> > >// After Remove() method> >foreach>(KeyValuePair<>int>,>string>>彼は>>My_dict)> >{> >Console.WriteLine('{0} and {1}',> >ele.Key, ele.Value);> >}> >Console.WriteLine();> > > >// Using Clear() method> >My_dict.Clear();> > >Console.WriteLine('Total number of key/value '+> >'pairs present>in> My_dict:{0}', My_dict.Count);> > >}> }>

>

>

出力:

1123 and Welcome 1124 and to 1125 and techcodeview.com 1124 and to 1125 and techcodeview.com Total number of key/value pairs present in My_dict:0>

辞書内の要素が利用可能かどうかを確認するにはどうすればよいですか?

Dictionary では、指定されたキーまたは値が指定された辞書に存在するかどうかを確認できます。 Dictionary クラスには、次の 2 つの異なるチェック メソッドが用意されています。

  • キーを含む : このメソッドは、ディクショナリに指定されたキーが含まれているかどうかを確認するために使用されます。
  • 値を含む : このメソッドは、辞書に特定の値が含まれているかどうかを確認するために使用されます。

例:

Cシャープ

Linuxコマンドでフォルダーを作成する




// C# program to illustrate how> // to check the given key or> // value present in the dictionary> // or not> using> System;> using> System.Collections.Generic;> class> GFG {> >// Main Method> >static> public> void> Main () {> > >// Creating a dictionary> >// using Dictionary class> >Dictionary<>int>,>string>>My_dict =>> >new> Dictionary<>int>,>string>>();>> > >// Adding key/value pairs in the> >// Dictionary Using Add() method> >My_dict.Add(1123, 'Welcome');> >My_dict.Add(1124, 'to');> >My_dict.Add(1125, 'techcodeview.com');> > >// Using ContainsKey() method to check> >// the specified key is present or not> >if> (My_dict.ContainsKey(1122)==>true>)> >{> >Console.WriteLine('Key>is> found...!!');> >}> >else> >{> >Console.WriteLine('Key>is> not found...!!');> >}> > >// Using ContainsValue() method to check> >// the specified value is present or not> >if> (My_dict.ContainsValue('techcodeview.com')==>true>)> >{> >Console.WriteLine('Value>is> found...!!');> >}> >else> >{> >Console.WriteLine('Value>is> not found...!!');> >}> >}> }>

>

>

出力:

Key is not found...!! Value is found...!!>