Search code examples
vb.netstring-comparison

What is the difference between vbNullString, String.Empty and ""?


All of these

  • txtUsername.Text <> vbNullString
  • txtUsername.Text <> String.Empty
  • txtUsername.Text <> ""

seem to return the same result. So, what's the difference between vbNullString, String.Empty and ""?


Solution

  • vbNullString is a constant, more likely out of VB6, String.Empty and "" are the same, some say that there is a performance difference, but that is not true, it is your choice what you use.

    To check whether a string is empty you can use If String.IsNullOrEmpty(String). The advantage is that it will also check for null, because string is a class and because of this a reference type.