Which is the best way to initialize a nullable string in vb.net
The string will be set to Nothing unless you specify otherwise, so there is no need to initialize it like this:
Dim s As String = Nothing
because it is the same as
Dim s As String
You might want to set it in your code, but not on initialization,
vbNullString
is a carry over from VB6 days and is functionally equivalent Nothing
Pure string ( ""
) and String.Empty
equivalent.
Some say it String.Empty
has a slight performance advantage, but I don't see any evidence.
Note: The IL generated 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 use the following if you want an empty string
Dim s As String = ""
And if you want a Null (Nothing) string:
Dim s As String
Which one you want depends on what you do
source to share
All three initializations you posted are functionally equivalent to an empty string or an uninitialized string. None of these will be beneficial over the others. And even if that were the case, the advantage would be so small that it wouldn't matter.
While individual views may differ, I feel that initializing variables to an empty / null value with a declaration is a bad thing (in general), unless this lifetime spans just a few lines. This is because it doesn't give the compiler any options to suggest optimization. Leaving things flexible for the compiler is especially useful in large maintenance projects where it's common to declare all the variables at the top of the sub / function and use them later in the code.
Consider the following situation:
Two variables are declared at the top of the function and are used somewhere in the code as follows.
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 in which these variables were used has been changed / removed and these variables are no longer needed. In such situations, it is common to forget to remove variable declarations. But if the declarations are not removed, the compiler will be able to warn for the variable str2
, but not for str1
.
For small functions, this may not seem important. But consider features where the size spans the page and everything doesn't fit on screen. In large maintenance projects, common functions grow. You get the edge there.
Explicitly assigning a null / null value when declaring variables doesn't give you much of the advantage of leaving it unassigned when you declare, which gives you the benefit mentioned above.
source to share
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 defaults to Nothing.
Rather than guaranteeing that this value is never anything by setting a value when you declare a variable, you must ensure that your code leaves no room for strWords to be Nothing, and if so, deal with it appropriately.
If you need to check if a string is empty or nothing, you can do:
If Not String.IsNullOrEmpty(strWords) Then
Credit to this post
source to share