C#のKeyNotFoundException
見つけたキーがDictionarycollectionで利用できない場合、KeyNotFoundExceptionがスローされます。
例を見てみましょう-
例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
try {
var dict = new Dictionary<string, string>() {
{"TV", "Electronics"},
{"Laptop", "Computers"},
};
Console.WriteLine(dict["Pen Drive"]);
}
catch (Exception e) {
Console.WriteLine(e);
}
}
} 以下は出力です。キーが辞書にないため、エラーKeyNotFoundExceptionが表示されます-
出力
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in <902ab9e386384bec9c07fa19aa938869>:0
-
ソートされた配列から重複を削除し、C#を使用して重複していない配列を返す方法は?
配列はすでにソートされているので、2つのポインターiiとjjを保持できます。ここで、iiは低速ランナーで、jjは高速ランナーです。 nums [i] =nums [j] nums [i] =nums [j]である限り、jjをインクリメントして重複をスキップします。 nums [j]!=nums [i]に遭遇すると、重複実行が終了したため、その値をnums [i + 1] nums [i+1]にコピーする必要があります。次にiiがインクリメントされ、jjが配列の最後に到達するまで同じプロセスを繰り返します。新しい配列を作成し、フィルター処理された配列からインデックスまですべての要素をコピーして
-
C#のKeyNotFoundException
見つけたキーがDictionarycollectionで利用できない場合、KeyNotFoundExceptionがスローされます。 例を見てみましょう- 例 using System; using System.Collections.Generic; public class Demo { public static void Main() { try { var dict = new Dictionary<string, string>()