VB.Net 中的只读局部变量 [英] Readonly local variable in VB.Net
问题描述
这是一个非常简单的问题,我很惊讶我不得不问它但是...
This is a really simple question and I'm suprised I have to ask it but...
在VB.Net中如何声明只读局部变量?
How does one declare a readonly local variable in VB.Net?
Java 和 C++ 有 final/const 局部变量,所以我确定 VB.Net 必须有它们,但我就是找不到它的语法.
Java and C++ have final/const local variables so I'm sure VB.Net must have them, but I just can't find the syntax for it.
推荐答案
不幸的是,VB.NET 只支持只读的字段,而不支持只读的局部变量.VB.NET 没有像 C++ 的 const
修饰符那样将变量标记为只读.
Unfortunately, VB.NET only supports readonly fields not readonly locals. VB.NET does not have anything like C++'s const
modifier to mark a variable as readonly.
根据变量的类型,Const
修饰符可能完成这项工作,但它的含义与 C++ 的 const
.在 VB.NET 中,Const
只是一个变量,其值在编译时已知,因此允许编译器用值本身替换源代码中该变量的所有用法.
Depending on the type of the variable, the Const
modifier might do the job but it doesn't mean the same thing as C++'s const
. In VB.NET, Const
is simply a variable whose value is known at compilation time, thus allowing the compiler to replace all usages of that variable in the source code with the value itself.
虽然编译器会阻止您修改 Const
变量,但您在选择可以标记为 Const
的类型方面受到严重限制,因为大多数类型无法提供编译时已知值.
While the compiler will prevent you from modifying a Const
variable you are severely limited in your options for the types that you can mark as Const
since most types cannot provide a known value at compilation time.
这篇关于VB.Net 中的只读局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!