在下面我得到一个编译时错误,说“使用未分配的本地变量匹配'” 如果我只是输入字符串匹配;但它适用于当我使用字符串匹配= 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 2012-03-15 17:55:35
在StackOverflow上仍然存在几个答案:只有一个 - > http://stackoverflow.com/questions/256073/c-sharp-error-use-of-unassigned -local-variable – AngeloBad 2012-03-15 17:58:11