VB.Net变量声明:类型或不键入?变量、或不、声明、类型

2023-09-03 05:13:32 作者:爱上你是我情非得已

在VB.Net,通常的方式来声明一个字符串将是:

In VB.Net, the usual way to declare a string would be :

Dim helloWorld As String = "Hello, World!"

不过,您也可以使用动态变量,如:

However, you can also use dynamic variables, such as :

Dim helloWorld = "Hello, World!"

无论最终会是同样的事情,但什么是最好的做法?

Both would end up to be the same thing, but what would be the best practice?

推荐答案

真的。 VB坐在净顶部和.Net的作品时,你最好坚持使用显式类型。唯一的例外是与选项推断打开(现在是默认设置),你也声明类似于C#的变量 VAR ,或者如果你正在做的COM互操作或其他什么东西,真正需要一个动态类型...这是的非常的罕见的。

Use a Type!

Really. VB sits on top of .Net, and .Net works best when you stick with explicit types. The only exceptions are with Option Infer turned on (it is now by default) and you also declare variables similar to C#'s var, or if you're doing COM interop or something else that truly requires a dynamic type... and that it is exceedingly rare.