C#のStringDictionaryクラス?
StringDictionayクラスは、オブジェクトではなく文字列として強く型付けされたキーと値を使用してハッシュテーブルを実装します。
StringDictionaryクラスのプロパティは次のとおりです-
| Sr.No | プロパティと説明 |
|---|---|
| 1 | カウント StringDictionary内のキーと値のペアの数を取得します。 |
| 2 | IsSynchronized StringDictionaryへのアクセスが同期されている(スレッドセーフ)かどうかを示す値を取得します。 |
| 3 | tem [String] 指定されたキーに関連付けられた値を取得または設定します。 |
| 4 | キー StringDictionaryのキーのコレクションを取得します。 |
| 5 | SyncRoot StringDictionaryへのアクセスを同期するために使用できるオブジェクトを取得します。 |
| 6 | 値 StringDictionaryの値のコレクションを取得します。 |
以下は、StringDictionaryクラスのメソッドの一部です-
| Sr.No | メソッドと説明 |
|---|---|
| 1 | Add(String、String) 指定されたキーと値を持つエントリをStringDictionaryに追加します。 |
| 2 | Clear() StringDictionaryからすべてのエントリを削除します。 |
| 3 | containsKey(String) StringDictionaryに特定の内容が含まれているかどうかを判別します |
| 4 | containsValue(String) StringDictionaryに特定の値が含まれているかどうかを判別します。 |
| 5 | CopyTo(Array、Int32) 文字列ディクショナリ値を、指定されたインデックスの1次元配列インスタンスにコピーします。 |
| 6 | Equals(Object) 指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判別します。(オブジェクトから継承) |
例
いくつかの例を見てみましょう-
2つのStringDictionaryオブジェクトが等しいかどうかを確認するためのコードは、次のとおりです-
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("A", "John");
strDict2.Add("B", "Andy");
strDict2.Add("C", "Tim");
strDict2.Add("D", "Ryan");
strDict2.Add("E", "Kevin");
strDict2.Add("F", "Katie");
strDict2.Add("G", "Brad");
StringDictionary strDict3 = new StringDictionary();
strDict3 = strDict2;
Console.WriteLine("Is Dictionary2 equal to Dictionary3? = "+strDict2.Equals(strDict3));
Console.WriteLine("Is Dictionary1 equal to Dictionary3? = "+strDict1.Equals(strDict3));
}
} 出力
これにより、次の出力が生成されます-
Is Dictionary2 equal to Dictionary3? = True Is Dictionary1 equal to Dictionary3? = False
例
StringDictionaryが同期されているかどうかを確認するためのコードは、次のとおりです-
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 elements...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 key-value pairs...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Is the StringDictionary2 synchronized? = "+strDict2.IsSynchronized);
}
} 出力
これにより、次の出力が生成されます-
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E Is the StringDictionary2 synchronized? = False
-
C#のコンソールクラス
C#のConsoleクラスは、コンソールアプリケーションの標準の入力、出力、およびエラーストリームを表すために使用されます。 C#のコンソールクラスプロパティの例をいくつか見てみましょう- Console.CursorLeftプロパティ C#でコンソールのCursorLeftを変更するには、Console.CursorLeftプロパティを使用します。 例 例を見てみましょう- using System; class Demo { public static void Main (string[] args) { Cons
-
C#のクラス
データ型のブループリントは、C#でクラスと呼ぶことができるものです。オブジェクトはクラスのインスタンスです。クラスを構成するメソッドと変数は、クラスのメンバーと呼ばれます。 例 以下は、C#のクラスの一般的な形式です- <access specifier> class class_name { // member variables <access specifier><data type> variable1; <access specifier><data