有趣的结果在VB.Net [英] Interesting Results In VB.Net
问题描述
对于i as Integer = 0到5
Dim sb As StringBuilder
如果i = 0
sb = New StringBuilder(i。 ToString())
下一页我是
Console.WriteLine(sb.ToString())
下一页我是
上面的代码给出了以下输出:
0
0
0
0
0
有点奇怪StringBuilder(或任何对象)在第一次迭代之后如何保存它状态环。 StringBuilder的文本保持不变并且
没有重新初始化。
评论?
Mythran
Mythran写道:For i As整数= 0到5
Dim sb As StringBuilder
如果i = 0
sb =新的StringBuilder(i.ToString())
接下来我
Console.WriteLine( sb.ToString())
接下来我上面的代码给出了以下输出:
0
0
0 > 0
0
有点奇怪StringBuilder(或任何对象)在循环的第一次迭代后如何保存它状态。 StringBuilder的文本保持不变,并且不会重新初始化。
评论?
Mythran
是否有任何保证短范围变量确实是短期寿命变量?它们可能与它们所处的
子例程或函数具有相同的生命周期,具体取决于编译器希望编译代码的方式。
Mythran写道:For i As Integer = 0 To 5
Dim sb As StringBuilder
如果i = 0
sb =新的StringBuilder(i.ToString())
接下来我
Console.WriteLine(sb.ToString())
下一个我>
上面的代码给出了以下输出:
0
0
0
0
0
有点奇怪的是StringBuilder(或任何对象)在循环的第一次迭代后如何保存它状态。 StringBuilder的文本保持不变并且
不会重新初始化。
评论?
不是真的令人惊讶...... VB.NET支持块范围。所以,你
在范围的开头声明一个引用类型(范围是
for / next):
Dim sb As StringBuilder
预留存储 - 但不初始化值。你只需要
初始化一次,当i = 0时。如果你修改了代码到
看起来像:
对于我作为整数= 0到5
Dim sb作为新的StringBuilder
如果i = 0那么
sb.Append(i.ToString( ))
结束如果
Console.WriteLine(sb)
下一页
然后你会得到了非常不同的结果。
-
汤姆谢尔顿[MVP]
< blockquote>
" Tom Shelton" < to*@mtogden.com>在消息中写道
news:11 ********************** @ j33g2000cwa.googlegr oups.com ...
Mythran写道:For i As Integer = 0 to 5
Dim sb As StringBuilder
如果i = 0
sb = New StringBuilder(i.ToString())
下一页我是
Console.WriteLine(sb.ToString())
接下来我上面的代码给出了以下输出:
0
0
0
0
有点奇怪StringBuilder(或任何对象)如何保存状态
循环的第一次迭代之后。 StringBuilder的文本保持不变,并且没有重新初始化。
评论?
不是那么令人惊讶...... VB.NET支持块范围。所以,你在范围的开头声明一个引用类型(范围是
for / next):
是的,不是很惊讶,但仍然有趣。我一直在想,它会被重新调暗(不是ReDim而是重新调整尺寸),因为在每次迭代结束时它将超出范围。
。 ..但我错了。它没有重新定义
。它的尺寸只有一次,并且似乎在每次迭代之后都没有看到它(我没有检查IL所以
不太确定)。 br />
Dim sb As StringBuilder
保留存储 - 但不初始化值。当i = 0时,你只需要初始化一次值。如果你修改了代码看起来像
对于i as Integer = 0到5
Dim sb As New StringBuilder
如果i = 0那么
sb.Append(i.ToString())
结束如果
Console.WriteLine(sb)
Next
然后你会得到非常不同的结果。
是的,我知道。在我的原始帖子之前,我也测试了它。那不是很有意思,但已经知道了嘿嘿:P
-
Tom Shelton [MVP]
感谢Tom的回复...
Mythran
For i As Integer = 0 To 5
Dim sb As StringBuilder
If i = 0
sb = New StringBuilder(i.ToString())
Next i
Console.WriteLine(sb.ToString())
Next i
The above code gives the following output:
0
0
0
0
0
Kinda wierd how the StringBuilder (or any object) saves it state after the
first iteration of the loop. The StringBuilder''s text stays the same and
doesn''t re-initialize.
Comments?
Mythran
Mythran wrote:For i As Integer = 0 To 5
Dim sb As StringBuilder
If i = 0
sb = New StringBuilder(i.ToString())
Next i
Console.WriteLine(sb.ToString())
Next i
The above code gives the following output:
0
0
0
0
0
Kinda wierd how the StringBuilder (or any object) saves it state after the
first iteration of the loop. The StringBuilder''s text stays the same and
doesn''t re-initialize.
Comments?
Mythran
Is there any guarantee that short-scoped variables are really
short-life variables? They may have the same life-time as the
subroutine or function they are in, depending upon how the compiler
wishes to compile the code.
Mythran wrote:For i As Integer = 0 To 5
Dim sb As StringBuilder
If i = 0
sb = New StringBuilder(i.ToString())
Next i
Console.WriteLine(sb.ToString())
Next i
The above code gives the following output:
0
0
0
0
0
Kinda wierd how the StringBuilder (or any object) saves it state after the
first iteration of the loop. The StringBuilder''s text stays the same and
doesn''t re-initialize.
Comments?
Not really that suprising... VB.NET supports block scope. So, you
declare a reference type at the begining of the scope (the scope being
the for/next):
Dim sb As StringBuilder
That reserves storage - but does not initialize the value. You only
initialize the value once, when i = 0. If you had modified the code to
look like:
For i As Integer = 0 To 5
Dim sb As New StringBuilder
If i = 0 Then
sb.Append(i.ToString())
End If
Console.WriteLine(sb)
Next
Then you would have gotten very different results.
--
Tom Shelton [MVP]
"Tom Shelton" <to*@mtogden.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Mythran wrote:For i As Integer = 0 To 5
Dim sb As StringBuilder
If i = 0
sb = New StringBuilder(i.ToString())
Next i
Console.WriteLine(sb.ToString())
Next i
The above code gives the following output:
0
0
0
0
0
Kinda wierd how the StringBuilder (or any object) saves it state after
the
first iteration of the loop. The StringBuilder''s text stays the same and
doesn''t re-initialize.
Comments?
Not really that suprising... VB.NET supports block scope. So, you
declare a reference type at the begining of the scope (the scope being
the for/next):
Aye, not suprising, but still interesting. I have been thinking all along
that it gets re-dimmed (not ReDim but re-dimensioned) because it would be
out of scope at the end of each iteration...but I had been mistaken. It
does NOT get re-dimensioned. It gets dimensioned a single time and seem to
not even look at it after every iteration after (I haven''t check the IL so
not too sure).
Dim sb As StringBuilder
That reserves storage - but does not initialize the value. You only
initialize the value once, when i = 0. If you had modified the code to
look like:
For i As Integer = 0 To 5
Dim sb As New StringBuilder
If i = 0 Then
sb.Append(i.ToString())
End If
Console.WriteLine(sb)
Next
Then you would have gotten very different results.
Yeah, I know. Before my original post, I tested that as well. That wasn''t
interesting though, already knew it heh :P
--
Tom Shelton [MVP]
Thanks for the reply Tom...
Mythran
这篇关于有趣的结果在VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!