私のコードは、特定の入力に対して Index out of Range 例外を与えていました。以下は問題のあるコードです。
string[] snippetElements = magic_string.Split('^');
string a = snippetElements[10] == null ? "" : "hello";
string b = snippetElements[11] == null ? "" : "world";
その特定の入力について、配列 snippetElements には要素が 1 つしかないため、10 番目と 11 番目の要素にインデックスを付けようとすると、例外が発生しました。
今のところ、次のチェックを導入しました。
if (snippetElements.Length >= 11)
{
string a = snippetElements[10] == null ? "" : "hello";
string b = snippetElements[11] == null ? "" : "world";
}
誰かがこの小切手を書くためのより良い方法を提案できますか? どういうわけか、数字の 11 はコード内で見栄えがよくありません。