以下では、文字列一致を入力するだけで、 "割り当てられていないローカル変数 'match'の使用" というコンパイル時エラーが表示されます。私は文字列の一致= nullを使用すると動作します。 違いは何ですか?一般的に、文字列に値を割り当てていない場合、このようにnullに割り当てる必要がありますか?nullに変数を代入すると何が行われますか?
string question = "Why do I need to assign to null";
char[] delim = { ' ' };
string[] strArr = question.Split(delim);
//Throws Error
string match;
//No Error
//string match = null;
foreach (string s in strArr)
{
if (s == "Why")
{
match = "Why";
}
}
Console.WriteLine(match);
コンパイラはあなたがそれを意味していることを知りたがっています: – Jodrell
まだStackOverflowにいくつかの回答があります:ただ一つ - > http://stackoverflow.com/questions/256073/c-sharp-error-use-of-unassigned -local-variable – AngeloBad