变量在循环中保留它们的值?
Variables retain their value in a loop?
如果我 运行 在控制台应用程序中使用这个简单的代码:
For i As Integer = 1 To 10
Dim s As String = i.ToString()
Dim x As Decimal
If i = 1 Then
x = Decimal.Parse(s)
End If
Console.WriteLine(x.ToString())
Next
Console.ReadLine()
出乎意料的是,x
保留了它的值 1,因此 1 被打印了 10 次。我认为循环的每次迭代都是它自己的代码块,并且状态没有延续?为什么会这样?我希望 x
具有默认值 System.Decimal
。
在 C# 中也会发生同样的事情,除了编译器不会让你在未初始化的变量上调用 ToString()
,但是如果你在 Visual Studio 中设置断点,你可以看到 x
保留其值 1。
for (int i = 1; i <= 10; i++)
{
string s = i.ToString();
Decimal x;
if(i == 1)
{
x = Decimal.Parse(s);
}
// Value of x remains 1
}
Console.ReadLine();
thought each iteration of the loop was its own code block, and that the state didn't carry over
它确实“结转”。
int i
的范围是循环的整个范围,值从 1 到 10。
if
语句仅在第一次迭代时执行,x
的作用域是 外部方法 的局部变量(假设循环是进入),不仅是循环体。话虽这么说,在循环之后 & 在循环之外,x == 1
也是如此。
其实循环中的Decimal x;
是多余的,你的执行逻辑和
是一样的
string s;
Decimal x;
for (int i = 1; i <= 10; i++)
{
s = i.ToString();
if(i == 1)
{
x = Decimal.Parse(s);
}
Console.WriteLine(x);
}
// x still defined here
为了理解你想要的东西,它应该是这样的:
For i As Integer = 1 To 10
If i = 1 Then
Console.WriteLine(Decimal.Parse(i.ToString()))
End If
Next
这会给你想要的相同结果,但它是可读的。
关于 VB.NET 请查看范围 here。在“Block Scope”部分有一条说明如下:
Even if the scope of a variable is limited to a block, its lifetime is
still that of the entire procedure. If you enter the block more than
once during the procedure, each block variable retains its previous
value. To avoid unexpected results in such a case, it is wise to
initialize block variables at the beginning of the block.
所以此行为是设计使然,您应该将变量初始化为您的代码需要的任何值。
我修改了你的代码以显示 x 第一次初始化为 0 但之后它保留值 1。
For i As Integer = 1 To 10
Dim s As String = i.ToString()
Dim x As Decimal
Console.WriteLine(x.ToString())
If i = 1 Then
x = Decimal.Parse(s)
End If
Console.WriteLine(x.ToString())
Next
Console.ReadLine()
如果我 运行 在控制台应用程序中使用这个简单的代码:
For i As Integer = 1 To 10
Dim s As String = i.ToString()
Dim x As Decimal
If i = 1 Then
x = Decimal.Parse(s)
End If
Console.WriteLine(x.ToString())
Next
Console.ReadLine()
出乎意料的是,x
保留了它的值 1,因此 1 被打印了 10 次。我认为循环的每次迭代都是它自己的代码块,并且状态没有延续?为什么会这样?我希望 x
具有默认值 System.Decimal
。
在 C# 中也会发生同样的事情,除了编译器不会让你在未初始化的变量上调用 ToString()
,但是如果你在 Visual Studio 中设置断点,你可以看到 x
保留其值 1。
for (int i = 1; i <= 10; i++)
{
string s = i.ToString();
Decimal x;
if(i == 1)
{
x = Decimal.Parse(s);
}
// Value of x remains 1
}
Console.ReadLine();
thought each iteration of the loop was its own code block, and that the state didn't carry over
它确实“结转”。
int i
的范围是循环的整个范围,值从 1 到 10。
if
语句仅在第一次迭代时执行,x
的作用域是 外部方法 的局部变量(假设循环是进入),不仅是循环体。话虽这么说,在循环之后 & 在循环之外,x == 1
也是如此。
其实循环中的Decimal x;
是多余的,你的执行逻辑和
string s;
Decimal x;
for (int i = 1; i <= 10; i++)
{
s = i.ToString();
if(i == 1)
{
x = Decimal.Parse(s);
}
Console.WriteLine(x);
}
// x still defined here
为了理解你想要的东西,它应该是这样的:
For i As Integer = 1 To 10
If i = 1 Then
Console.WriteLine(Decimal.Parse(i.ToString()))
End If
Next
这会给你想要的相同结果,但它是可读的。
关于 VB.NET 请查看范围 here。在“Block Scope”部分有一条说明如下:
Even if the scope of a variable is limited to a block, its lifetime is still that of the entire procedure. If you enter the block more than once during the procedure, each block variable retains its previous value. To avoid unexpected results in such a case, it is wise to initialize block variables at the beginning of the block.
所以此行为是设计使然,您应该将变量初始化为您的代码需要的任何值。
我修改了你的代码以显示 x 第一次初始化为 0 但之后它保留值 1。
For i As Integer = 1 To 10
Dim s As String = i.ToString()
Dim x As Decimal
Console.WriteLine(x.ToString())
If i = 1 Then
x = Decimal.Parse(s)
End If
Console.WriteLine(x.ToString())
Next
Console.ReadLine()