gpt4 book ai didi

.net - MSDN 文档错误? "If the procedure is Shared, all its local variables are automatically shared. This includes the Static variables."

转载 作者:行者123 更新时间:2023-12-02 08:49:20 24 4
gpt4 key购买 nike

我正在查看有关 VB.Net 很少使用的 MSDN 文档 Static局部变量的关键字。我的问题不是关于 Static 是如何工作的(我个人打算避免使用它,因为它似乎会给没有注意到副作用的 future 程序员带来悲伤。)我的问题是关于文档中的这个声明:

The behavior of any local variable depends on whether it is declared in a Shared procedure. If the procedure is Shared, all its local variables are automatically shared. This includes the Static variables. There is only one copy of such a variable for the whole application.

这似乎暗示 Shared Sub 中的所有局部变量都将表现得像静态变量——它们将在不同的调用中保持它们的值。但我知道事实并非如此,我写了一个小测试程序来证明这一点:

Class TestSharedSub

Shared Sub Main()
Test()
Test()
Test()
Console.Write("Press any key to continue...") : Console.ReadKey()
' Output:
' 1, 1
' 2, 1
' 3, 1
End Sub

Shared Sub Test()
Dim iNormal As Integer
Static iStatic As Integer
iNormal += 1
iStatic += 1
Console.WriteLine(iStatic & ", " & iNormal)
End Sub

End Class

那么谁能以一种有意义的方式为我解释上述陈述?或者这是文档中的错误?它自 VS 2005 版本的文档以来一直存在,并且仍然存在于 Visual Studio 11 版本中。

最佳答案

不是,是胡说八道。它在下一段中继续被完全破坏:

If the procedure is not Shared, its local variables are instance variables.

局部变量不是实例变量...

写那个页面的人似乎根本不了解局部变量。天知道他们会用递归做什么。请注意,它合理地开始:

Normally, a local variable in a procedure ceases to exist as soon as the procedure stops.

... 但是这个说法显然与后面的说法相矛盾。 叹气。

我建议您在 Connect 上提交错误.

关于.net - MSDN 文档错误? "If the procedure is Shared, all its local variables are automatically shared. This includes the Static variables.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9689839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com