以下函数生成错误,“使用未分配的局部变量‘intLast’。我不确定我做错了什么。
static string RemovePath(string strInput)
{
int intLast;
for (int i = 1; i < strInput.Length; i++)
{
if (strInput.Substring(i, 1) == @"\")
{
intLast = i;
}
}
string strOutput = strInput.Substring(strInput.Length - intLast);
return strOutput;
}
最佳答案
您正在以下行中使用变量 intLast
:
string strOutput = strInput.Substring(strInput.Length - intLast);
但变量只有在某些条件下才会有值(strInput.Length > 1
和 strInput.Substring(i, 1) == @"\"
) .因此错误。
要解决此问题,请在声明中提供默认值:
int intLast = 0;//或任何默认值。
关于c# - 局部变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15569634/