What's better then somestring=""

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**Developer**

I understand that

If somestring.length = 0 Then

is faster then

If somestring = "" Then

and is equivalent to

If (somestring is Nothing) AndAlso (somestring = "") Then

Is that correct???



Also is there something better then:

somestring = ""



Thanks
 
somestring = string.empty should be faster than using "", since anytime you
use double quotes, new string object is created in memory, but when you use
string.empty, you are comparing against a known constant.
 
somestring = string.empty should be faster than using "", since anytime you
use double quotes, new string object is created in memory, but when you use
string.empty, you are comparing against a known constant.

No, you'll get the same empty string object in both cases. Try this

Dim s1 As String = ""
Dim s2 As String = ""
Console.WriteLine(s1 Is s2)
Console.WriteLine(s1 Is String.Empty)


Mattias
 
Developer,

In my idea is better not to think about performances got by this, it will
probably be something as 0,0000001 nanosecond and in the future even less.

You can in your live never win the time, that you did spent thinking about
it.

Just my idea

Cor
 
Herfried,
Yeah, but 'String.Empty' is the same as "" (a zero-length string). In
terms of readability I perfer "" over 'String.Empty'.
I prefer it because of typing

:-))

Cor
 
Mattias Sjögren said:
No, you'll get the same empty string object in both cases. Try this

Dim s1 As String = ""
Dim s2 As String = ""
Console.WriteLine(s1 Is s2)
Console.WriteLine(s1 Is String.Empty)

That's true. The empty string is interned, and thus not duplicated:

\\\
MsgBox(String.IsInterned("") Is Nothing) ' False.
///
 
**Developer** said:
I understand that

If somestring.length = 0 Then

is faster then

If somestring = "" Then

There is a big difference between the two approaches. The first one will
crash with a 'NullReferenceException' if 'somestring' contains a
null-reference. The latter approach won't crash, it will simply return
'False' for the expression 'somestring = ""' because a reference to '""' is
not the same as a reference to 'Nothing'.
and is equivalent to

If (somestring is Nothing) AndAlso (somestring = "") Then

Is that correct???

No, that's not correct.

Personally I prefer to use the 'Len' function because it will return 0 as
length even for null-references:

\\\
Dim s As String = Nothing
MsgBox(Len(s)) ' 0.
s = ""
MsgBox(Len(s)) ' 0.
///

If you want an exception to be thrown for a null-reference, use the 'String'
object's 'Length' property.
 
I now understand the difference between the approaches and can decide for
each instance.

Thanks for all the replies
 
445: If t = "" Then
00000049 6A 00 push 0
0000004b 8B 15 38 10 09 06 mov edx,dword ptr ds:[06091038h]
00000051 8B 4D D4 mov ecx,dword ptr [ebp-2Ch]
00000054 FF 15 50 8C 91 00 call dword ptr ds:[00918C50h]
0000005a 8B F0 mov esi,eax
0000005c 85 F6 test esi,esi
0000005e 75 10 jne 00000070

and the other
448: If t = String.Empty Then
00000067 6A 00 push 0
00000069 8B 15 10 20 09 06 mov edx,dword ptr ds:[06092010h]
0000006f 8B 4D D4 mov ecx,dword ptr [ebp-2Ch]
00000072 FF 15 50 8C 91 00 call dword ptr ds:[00918C50h]
00000078 8B F0 mov esi,eax
0000007a 85 F6 test esi,esi
0000007c 75 10 jne 0000008E

As you can see, we have the same code and both call the
Microsoft.VisualBasic.CompilerServices.StringType.StrCmp for the compare. For
me, that is enought, but I usually use string.empty.
 
**Developer**
Short answer: No

Long Answer: As you've noticed: You will receive an answer for every
developer out there. ;-)

I only use "somestring.Length = 0" when I need to check the length of a
string, rarely do I need to check the length of a string, normally I need to
use the length of a string.

I normally use "something = "" " or sometimes "something = string.empty"
when I need to see if I have an empty string. VB automatically checks
something for nothing in addition to being empty (as = calls the VB.StrCmp
routine)

I only use "something Is Nothing" when I explicitly need to know if there is
a string there or not. Normally I use this as guard conditions in
constructors & methods to throw ArgumentNullExceptions, Something Like:

Public Class Something

Private Readonly m_name As String

Public Sub New(name As String)
If name Is Nothing Then Throw New ArgumentNullException("name")
m_name = name
End Sub

Public Sub PlayWith(other As String)
If other Is Nothing Then Throw New
ArgumentNullException("other")

' we can use instance methods on both other & m_name here
' without fear of NullReferenceExceptions...
End Sub

End Class

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I understand that
|
| If somestring.length = 0 Then
|
| is faster then
|
| If somestring = "" Then
|
| and is equivalent to
|
| If (somestring is Nothing) AndAlso (somestring = "") Then
|
| Is that correct???
|
|
|
| Also is there something better then:
|
| somestring = ""
|
|
|
| Thanks
|
|
 
Jay B. Harlow said:
**Developer**
Short answer: No

Long Answer: As you've noticed: You will receive an answer for every
developer out there. ;-)

I only use "somestring.Length = 0" when I need to check the length of a
string, rarely do I need to check the length of a string, normally I need
to
use the length of a string.

I normally use "something = "" " or sometimes "something = string.empty"
when I need to see if I have an empty string. VB automatically checks
something for nothing in addition to being empty (as = calls the VB.StrCmp
routine)


Jay,
does all this agree with Adelino's reply(especially the
"as = calls the VB.StrCmp routine" statement)

If not, please comment

As always, Thanks


I only use "something Is Nothing" when I explicitly need to know if there
is
a string there or not. Normally I use this as guard conditions in
constructors & methods to throw ArgumentNullExceptions, Something Like:

Public Class Something

Private Readonly m_name As String

Public Sub New(name As String)
If name Is Nothing Then Throw New ArgumentNullException("name")
m_name = name
End Sub

Public Sub PlayWith(other As String)
If other Is Nothing Then Throw New
ArgumentNullException("other")

' we can use instance methods on both other & m_name here
' without fear of NullReferenceExceptions...
End Sub

End Class

--
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I understand that
|
| If somestring.length = 0 Then
|
| is faster then
|
| If somestring = "" Then
|
| and is equivalent to
|
| If (somestring is Nothing) AndAlso (somestring = "") Then
|
| Is that correct???
|
|
|
| Also is there something better then:
|
| somestring = ""
|
|
|
| Thanks
|
|
 
Adelino Araújo said:
445: If t = "" Then
00000049 6A 00 push 0
0000004b 8B 15 38 10 09 06 mov edx,dword ptr ds:[06091038h]
00000051 8B 4D D4 mov ecx,dword ptr [ebp-2Ch]
00000054 FF 15 50 8C 91 00 call dword ptr ds:[00918C50h]
0000005a 8B F0 mov esi,eax
0000005c 85 F6 test esi,esi
0000005e 75 10 jne 00000070

and the other
448: If t = String.Empty Then
00000067 6A 00 push 0
00000069 8B 15 10 20 09 06 mov edx,dword ptr ds:[06092010h]
0000006f 8B 4D D4 mov ecx,dword ptr [ebp-2Ch]
00000072 FF 15 50 8C 91 00 call dword ptr ds:[00918C50h]
00000078 8B F0 mov esi,eax
0000007a 85 F6 test esi,esi
0000007c 75 10 jne 0000008E

As you can see, we have the same code and both call the
Microsoft.VisualBasic.CompilerServices.StringType.StrCmp for the compare.
ACK.

For me, that is enought, but I usually use string.empty.

'String.Empty' is harder to type than "", thus I prefer the latter.
However, I believe that it should be up to personal preference to choose one
over the other.
 
**Developer**
| > I normally use "something = "" " or sometimes "something = string.empty"
| > when I need to see if I have an empty string. VB automatically checks
| > something for nothing in addition to being empty (as = calls the
VB.StrCmp
| > routine)
| does all this agree with Adelino's reply(especially the
| "as = calls the VB.StrCmp routine" statement)

Naturally as StrCmp is the function call that checks the arguments for
Nothing... Plus it takes into account your Option Compare setting...

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|
| message | > **Developer**
| > Short answer: No
| >
| > Long Answer: As you've noticed: You will receive an answer for every
| > developer out there. ;-)
| >
| > I only use "somestring.Length = 0" when I need to check the length of a
| > string, rarely do I need to check the length of a string, normally I
need
| > to
| > use the length of a string.
| >
| > I normally use "something = "" " or sometimes "something = string.empty"
| > when I need to see if I have an empty string. VB automatically checks
| > something for nothing in addition to being empty (as = calls the
VB.StrCmp
| > routine)
|
|
| Jay,
| does all this agree with Adelino's reply(especially the
| "as = calls the VB.StrCmp routine" statement)
|
| If not, please comment
|
| As always, Thanks
|
|
|
| >
| > I only use "something Is Nothing" when I explicitly need to know if
there
| > is
| > a string there or not. Normally I use this as guard conditions in
| > constructors & methods to throw ArgumentNullExceptions, Something Like:
| >
| > Public Class Something
| >
| > Private Readonly m_name As String
| >
| > Public Sub New(name As String)
| > If name Is Nothing Then Throw New
ArgumentNullException("name")
| > m_name = name
| > End Sub
| >
| > Public Sub PlayWith(other As String)
| > If other Is Nothing Then Throw New
| > ArgumentNullException("other")
| >
| > ' we can use instance methods on both other & m_name here
| > ' without fear of NullReferenceExceptions...
| > End Sub
| >
| > End Class
| >
| > --
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > |I understand that
| > |
| > | If somestring.length = 0 Then
| > |
| > | is faster then
| > |
| > | If somestring = "" Then
| > |
| > | and is equivalent to
| > |
| > | If (somestring is Nothing) AndAlso (somestring = "") Then
| > |
| > | Is that correct???
| > |
| > |
| > |
| > | Also is there something better then:
| > |
| > | somestring = ""
| > |
| > |
| > |
| > | Thanks
| > |
| > |
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top