C#で指定されたキーと値を使用してOrderedDictionaryに新しいエントリを挿入します
指定されたキーと値を使用してOrderedDictionaryに新しいエントリを挿入するには、コードは次のとおりです-
例
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
dict.Add("6", "Six");
dict.Add("7", "Seven");
dict.Add("8", "Eight");
Console.WriteLine("Elements...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
dict.Insert(7, "15", "Fifteen");
Console.WriteLine("Elements...UPDATED");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
} 出力
これにより、次の出力が生成されます-
Elements... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Key = 7, Value = Seven Key = 8, Value = Eight Elements...UPDATED Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Key = 7, Value = Seven Key = 15, Value = Fifteen Key = 8, Value = Eight
例
別の例を見てみましょう-
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Laptop");
dict.Add("B", "Desktop");
Console.WriteLine("Elements...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
dict.Add("C", "Ultrabook");
dict.Add("D", "Alienware");
Console.WriteLine("\nElements...UPDATED");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
dict.Insert(2, "K", "Speakers");
Console.WriteLine("\nElements...UPDATED");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
} 出力
これにより、次の出力が生成されます-
Elements... Key = A, Value = Laptop Key = B, Value = Desktop Elements...UPDATED Key = A, Value = Laptop Key = B, Value = Desktop Key = C, Value = Ultrabook Key = D, Value = Alienware Elements...UPDATED Key = A, Value = Laptop Key = B, Value = Desktop Key = K, Value = Speakers Key = C, Value = Ultrabook Key = D, Value = Alienware
-
C#でのハッシュテーブルと辞書の操作
ハッシュテーブル ハッシュテーブルクラスは、キーのハッシュコードに基づいて編成されたキーと値のペアのコレクションを表します。キーを使用してコレクション内の要素にアクセスします。 Hashtableクラスで一般的に使用されるメソッドのいくつかは-です。 Sr.No。 メソッドと説明 1 public virtual void Add(object key、object value); 指定されたキーと値を持つ要素をハッシュテーブルに追加します。 2 public virtual void Clear(); ハッシュテーブルからすべての要素を削除しま
-
Redis GETSET –Redisでキーの新しい文字列値と古い文字列値を設定する方法
このチュートリアルでは、キーの新しい文字列値を設定し、古い文字列値を返す方法について学習します。このために、Redisの GETSETを使用します コマンド。 GETSETコマンド このコマンドは、指定されたキーに新しい文字列値を設定し、古い文字列値を返します。キーが存在しない場合はNILを返し、キーは存在するがキーに格納されている値が文字列でない場合はエラーを返します。 RedisGETSETコマンドの構文は次のとおりです。- 構文:- redis host:post> GETSET <keyname> <value> 出力:- (string) r