C#のListDictionaryクラス
ListDictionaryクラスは、単一リンクリストを使用してIDictionaryを実装します。通常、アイテムが10個未満のコレクションに推奨されます。
ListDictionaryクラスのプロパティは次のとおりです-
| Sr.No | プロパティと説明 |
|---|---|
| 1 | カウント ListDictionaryに含まれるキーと値のペアの数を取得します。 |
| 2 | IsFixedSize ListDictionaryのサイズが固定されているかどうかを示す値を取得します。 |
| 3 | IsReadOnly ListDictionaryが読み取り専用かどうかを示す値を取得します。 |
| 4 | IsSynchronized ListDictionaryが同期されている(スレッドセーフ)かどうかを示す値を取得します。 |
| 5 | アイテム[オブジェクト] 指定された値に関連付けられた値を取得または設定します。 |
| 6 | キー ListDictionaryのキーを含むICollectionを取得します。 |
| 7 | SyncRoot ListDictionaryへのアクセスを同期するために使用できるオブジェクトを取得します。 |
| 8 | 値 ListDictionaryの値を含むICollectionを取得します。 |
以下は、ListDictionaryクラスのメソッドの一部です-
| Sr.No | メソッドと説明 |
|---|---|
| 1 | Add(Object、Object) 指定されたキーと値を持つエントリをListDictionaryに追加します。 |
| 2 | Clear() ListDictionaryからすべてのエントリを削除します。 |
| 3 | contains(Object) ListDictionaryに特定のキーが含まれているかどうかを判別します。 |
| 4 | CopyTo(Array、Int32) ListDictionaryエントリを、指定されたインデックスの1次元配列インスタンスにコピーします。 |
| 5 | Equals(Object) 指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判別します。 (オブジェクトから継承) |
| 6 | GetEnumerator() ListDictionaryを反復処理するIDictionaryEnumeratorを返します。 |
| 7 | GetHashCode() デフォルトのハッシュ関数として機能します。 (オブジェクトから継承) |
| 8 | GetType() 現在のインスタンスのタイプを取得します。 (オブジェクトから継承) |
例
いくつかの例を見てみましょう-
ListDictionaryが読み取り専用かどうかを確認するには、コードは次のとおりです-
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 elements...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Is the ListDictionary1 having fixed size? = "+dict1.IsFixedSize);
Console.WriteLine("If ListDictionary1 read-only? = "+dict1.IsReadOnly);
Console.WriteLine("Is ListDictionary1 synchronized = "+dict1.IsSynchronized);
Console.WriteLine("The ListDictionary1 has the key M? = "+dict1.Contains("M"));
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nListDictionary2 key-value pairs...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = "+ demoEnum.Value);
Console.WriteLine("Is the ListDictionary2 having fixed size? = "+dict2.IsFixedSize);
Console.WriteLine("If ListDictionary2 read-only? = "+dict2.IsReadOnly);
Console.WriteLine("Is ListDictionary2 synchronized = "+dict2.IsSynchronized);
Console.WriteLine("The ListDictionary2 has the key 5? = "+dict2.Contains("5"));
}
} 出力
これにより、次の出力が生成されます-
ListDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Is the ListDictionary1 having fixed size? = False If ListDictionary1 read-only? = False Is ListDictionary1 synchronized = False The ListDictionary1 has the key M? = False ListDictionary2 key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Is the ListDictionary2 having fixed size? = False If ListDictionary2 read-only? = False Is ListDictionary2 synchronized = False The ListDictionary2 has the key 5? = True
2つのListDictionaryオブジェクトが等しいかどうかを確認するためのコードは、次のとおりです-
例
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 elements...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nListDictionary2 elements...");
foreach(DictionaryEntry d in dict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict3 = new ListDictionary();
dict3 = dict2;
Console.WriteLine("\nIs ListDictionary3 equal to ListDictionary2? = "+(dict3.Equals(dict2)));
}
} 出力
これにより、次の出力が生成されます-
ListDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear ListDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Is ListDictionary3 equal to ListDictionary2? = True
-
C#のコンソールクラス
C#のConsoleクラスは、コンソールアプリケーションの標準の入力、出力、およびエラーストリームを表すために使用されます。 C#のコンソールクラスプロパティの例をいくつか見てみましょう- Console.CursorLeftプロパティ C#でコンソールのCursorLeftを変更するには、Console.CursorLeftプロパティを使用します。 例 例を見てみましょう- using System; class Demo { public static void Main (string[] args) { Cons
-
Pythonでのオブジェクト指向プログラミング?
Pythonは、その存在以来、オブジェクト指向プログラミング言語でした。クラスとオブジェクトは、オブジェクト指向プログラミングの2つの主要な構成要素です。 クラスは、オブジェクトがクラスのインスタンスである新しいタイプのオブジェクトを作成します。 最も単純なクラスの1つを作成しましょう Pythonでクラスを定義する 空のクラスを定義しましょう。 #Define a class class Vehicle(): pass # An empty block # Instantiating objects v = Vehicle() print(v) 結果 <