简体   繁体   中英

which is the best way to initialize a string with null in vb.net

We can declare and initialize a string variable in vb.net in following ways:

 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?

A string will be set to Nothing until you specify otherwise, so there is no need to initialise it like this:

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,

vbNullString is a carry over from VB6 days and is functionally equivalent to Nothing

A blank string ( "" ) and String.Empty are equivalent.


Some say String.Empty has a slight performance advantage but I don't see any evidence.

Note: The IL produced by these two is different:

"" produces:

IL_0001:  ldstr      ""

String.Empty produces:

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:

Dim s As String

Which one you want depends on what you are doing

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 str2 variable, but not for 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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM