使用未分配的值解释。意外行为
Using unassigned value explanation. Unexpected behavior
public static void Main()
{
int n;
//n++; Of course it would cause 'Use of unassigned local variable error' compiler error.
int.TryParse("not an int", out n); //not assignig here
n++; //now legal. Why?
System.Console.WriteLine(n); //1
}
我不明白为什么这段代码会这样。一开始它不允许使用未分配的变量,但在 TryParse 之后它允许使用,尽管 TryParse 不分配任何变量。在某些时候变量被分配给默认值 0(我想从一开始)但是这种行为的逻辑和解释是什么?
反编译的 C# int.TryParse
定义:
public static bool TryParse(string s, out int result)
{
return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
// System.Number
internal unsafe static bool TryParseInt32(string s, NumberStyles style, NumberFormatInfo info, out int result)
{
byte* stackBuffer = stackalloc byte[1 * 114 / 1];
Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
result = 0;
if (!Number.TryStringToNumber(s, style, ref numberBuffer, info, false))
{
return false;
}
if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
{
if (!Number.HexNumberToInt32(ref numberBuffer, ref result))
{
return false;
}
}
else
{
if (!Number.NumberToInt32(ref numberBuffer, ref result))
{
return false;
}
}
return true;
}
如您所见,result
设置为 0
,这是因为 "not an int"
实际上...不是 int
。对于失败的转换尝试 (如果你想这样看),此函数将结果设置为 0
.
如this other question所述,局部变量未初始化。
When this method returns, [result]contains the 32-bit signed integer value equivalent of the number contained in s[entry string], if the conversion succeeded, or zero if the conversion failed.
所以在tryparse之后,n被初始化为值0。
public static void Main()
{
int n;
//n++; Of course it would cause 'Use of unassigned local variable error' compiler error.
int.TryParse("not an int", out n); //not assignig here
n++; //now legal. Why?
System.Console.WriteLine(n); //1
}
我不明白为什么这段代码会这样。一开始它不允许使用未分配的变量,但在 TryParse 之后它允许使用,尽管 TryParse 不分配任何变量。在某些时候变量被分配给默认值 0(我想从一开始)但是这种行为的逻辑和解释是什么?
反编译的 C# int.TryParse
定义:
public static bool TryParse(string s, out int result)
{
return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}
// System.Number
internal unsafe static bool TryParseInt32(string s, NumberStyles style, NumberFormatInfo info, out int result)
{
byte* stackBuffer = stackalloc byte[1 * 114 / 1];
Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
result = 0;
if (!Number.TryStringToNumber(s, style, ref numberBuffer, info, false))
{
return false;
}
if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
{
if (!Number.HexNumberToInt32(ref numberBuffer, ref result))
{
return false;
}
}
else
{
if (!Number.NumberToInt32(ref numberBuffer, ref result))
{
return false;
}
}
return true;
}
如您所见,result
设置为 0
,这是因为 "not an int"
实际上...不是 int
。对于失败的转换尝试 (如果你想这样看),此函数将结果设置为 0
.
如this other question所述,局部变量未初始化。
When this method returns, [result]contains the 32-bit signed integer value equivalent of the number contained in s[entry string], if the conversion succeeded, or zero if the conversion failed.
所以在tryparse之后,n被初始化为值0。