Ping
Ping

Reputation: 103

string.IsNullOrEmpty vs string = '' in Visual Basic

What is the different when using string.IsNullOrEmpty and string = ''

If  string.IsNullOrEmpty(str) then
   ' do something
End If

If string = "" then
   ' do something
End If

Does IsNullorEmpty include ""?

Upvotes: 0

Views: 1982

Answers (2)

TDL
TDL

Reputation: 126

This is a good reference for the built-in function IsNullorEmpty of the String class:

http://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx

The difference is that the latter uses a built-in function to return True or False in checking strings if they are empty. Basically the second condition is a straight-forward checking of a String if it is empty.

IsNullorEmpty does not include " in its syntax. Hope that helps.

Upvotes: 1

Dan
Dan

Reputation: 1001

The String.IsNullOrEmpty method checks for both null and empty fields, while string=="" only checks for empty fields.

If a string is not defined, it will be null by default. Therefore, it is a better idea to use IsNullOrEmpty.

P.S. 'a' and "a" is different. The single quote is used for characters while double quotes are used for strings.

Upvotes: 1

Related Questions