这是在 vb.net 中用 null 初始化字符串的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25401202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
which is the best way to initialize a string with null in vb.net
提问by Matt Wilko
We can declare and initialize a stringvariable in vb.netin following ways:
我们可以通过以下方式声明和初始化string变量vb.net:
Dim str1 As String = ""
Dim str2 As String = Nothing
Dim str3 As String = vbNullString
Question :
题 :
- Which one is the best programming practice?
- What is the difference between these three methods?
- 哪一种是最好的编程实践?
- 这三种方法有什么区别?
回答by Matt Wilko
A string will be set to Nothing until you specify otherwise, so there is no need to initialise it like this:
一个字符串将被设置为 Nothing ,直到你另外指定,所以没有必要像这样初始化它:
Dim s As String = Nothing
because this is just the same as
因为这和
Dim s As String
You may want to set it Nothing later on in your code but not at initialisation,
您可能希望稍后在代码中设置它,但不要在初始化时设置它,
vbNullStringis a carry over from VB6 days and is functionally equivalent to Nothing
vbNullString是 VB6 天的结转,在功能上等同于 Nothing
A blank string ("") and String.Emptyare equivalent.
空字符串 ( "") 和String.Empty是等价的。
Some say String.Emptyhas a slight performance advantage but I don't see any evidence.
有人说String.Empty有轻微的性能优势,但我没有看到任何证据。
Note: The IL produced by these two isdifferent:
注意:这两者产生的IL是不同的:
""produces:
""产生:
IL_0001: ldstr ""
String.Emptyproduces:
String.Empty产生:
IL_0001: ldsfld string [mscorlib]System.String::Empty
So to answer your question:
所以要回答你的问题:
Dim str1 As String = "" 'Initialises as blank string same as String.Empty
Dim str2 As String = Nothing 'Same as Dim str2 As String
Dim str3 As String = vbNullString 'Same as previous line but deprecated so avoid
So you should be using the following if you want an empty string
所以如果你想要一个空字符串,你应该使用以下内容
Dim s As String = ""
And the following if you want a Null (Nothing) string:
如果您想要 Null (Nothing) 字符串,请执行以下操作:
Dim s As String
Which one you want depends on what you are doing
你想要哪个取决于你在做什么
回答by Pradeep Kumar
All the three initializations you posted are functionally equivalent to an empty string or uninitialized string. None of them will be advantageous over the others. And even if it has, the advantage would be so small that it won't matter at all.
您发布的所有三个初始化在功能上都等效于空字符串或未初始化的字符串。他们中的任何一个都不会比其他人更有优势。即使有,优势也会小到根本无关紧要。
While individual views may differ, I feel initializing variables with empty/null value with declaration is a bad thing (in general), unless that variable's lifetime spans just a few lines. This is because it doesn't give the compiler any options for suggesting optimizations. Leaving things flexible for the compiler is particularly useful in big maintenance projects, where it is common to declare all variables at the top of the sub/function and use it later in the code.
虽然个人观点可能不同,但我觉得用声明的空/空值初始化变量是一件坏事(一般来说),除非该变量的生命周期仅跨越几行。这是因为它没有为编译器提供任何建议优化的选项。为编译器留出灵活性在大型维护项目中特别有用,通常在子/函数顶部声明所有变量并稍后在代码中使用它。
Consider the following situation:
考虑以下情况:
Two variables are declared at the top of a function and used somewhere in the code in the following way.
在函数顶部声明了两个变量,并按以下方式在代码中的某处使用。
Sub Test()
Dim str1 As String = ""
Dim str2 As String
' blah blah blah
' some code
' more code
' variables are used here
str1 = "something"
str2 = "something else"
End Sub
Now due to some maintenance requirements, the code where these variables were used is modified/removed and these variables are no longer needed. It is common to forget to remove the variable declarations in such situations. But if the declarations are not removed, the compiler would be able to warn for the str2variable, but not for str1.
现在由于一些维护要求,使用这些变量的代码被修改/删除,不再需要这些变量。在这种情况下,忘记删除变量声明是很常见的。但是,如果不删除声明,编译器将能够针对str2变量发出警告,但不能针对str1.


For small functions this may not seem any good. But consider functions where the size spans in pages and everything doesn't fit on the screen. It is common for functions to grow in size in large maintenance projects. You get an advantage there.
对于小功能,这似乎没什么好处。但是考虑一下尺寸跨越页面并且所有内容都不适合屏幕的功能。在大型维护项目中,功能的规模增长是很常见的。你在那里获得优势。
While assigning empty/null value explicitly while declaring variables don't give you any special advantage, leaving it unassigned when declaring it gives you the advantage mentioned above.
虽然在声明变量时显式分配空/空值不会给您带来任何特殊优势,但在声明时不分配它会给您带来上述优势。
回答by Jonny
Dim str As String = "" ' <--This is "not best practice"
Dim str2 As String = Nothing ' <--This is "not best practice"
Dim str3 As String = vbNullString ' <--This is "not best practice"
Dim str4 As String = String.Empty ' <--correct
By default, strWords will be Nothing by default anyway.
默认情况下, strWords 无论如何都会默认为 Nothing。
Rather than ensuring this value is never nothing by setting a value when declaring the variable, you should ensure your code doesn't leave the possibility of strWords being Nothing, and if it is, dealing with it appropriately.
与其在声明变量时通过设置值来确保此值永远不会为空,您应该确保您的代码不会留下 strWords 为空的可能性,如果是,请适当处理它。
If you need to check whether a string is not empty or nothing, you can do:
如果您需要检查字符串是否为空或不为空,您可以执行以下操作:
If Not String.IsNullOrEmpty(strWords) Then
Credit to this post
归功于这篇文章

