What is the difference between ("") and (null)

When trying to set checks, I first encountered some problems with checking if the text field is null, I tried to use

private void btnGo_Click(object sender, EventArgs e) { string name = textLogin.Text; if (name == null) { labelError.Visiblle = true; labelError.Text = "Field Cannot be Left Blank" } } 

but it didn’t work until I tried it

  private void btnGo_Click(object sender, EventArgs e) { string name = textLogin.Text; if (name == "") { labelError.Visiblle = true; labelError.Text = "Field Cannot be Left Blank" } } 

My question is: I want to know the difference between ("") and (null) and why null did not work.

Thanks in advance

+8
c # winforms
source share
9 answers

Same as the difference between 0 and an empty array; everything. Theyre different meanings. "" is an empty string, and this is what an empty text box is executing because text is everything. null not a value and is not that an empty text field has Text .

+11
source share

"" is an empty string vs null , which means that "does not exist".

In your case, you first compared name to "does not exist", which was false, because name really existed. Then you compared name to an empty string, which is true because it has the value of an empty string.

+4
source share

The .NET System.String data type is a class , a reference type. Thus, an empty string ( "" or string.Empty ) is a reference to a value with a zero length, while null does not refer to a real value, so any attempt to access the reference position that it refers to fails.

For example:

 string emptyString = ""; string nullString = null; Console.WriteLine(emptyString.Length); // 0 Console.WriteLine(nullString.Length); // Exception! 

I would recommend that you use IsNullOrEmpty (or IsNullOrWhiteSpace ) in your validation code to handle both cases:

 if (string.IsNullOrEmpty(name)) { labelError.Visiblle = true; labelError.Text = "Field Cannot be Left Blank" } 
+3
source share

The default value of TextBox.Text is String.Empty or "" not null. So your first code did not work. null is just an indication that the object is not pointing to anything, it is not allocated by any memory.

+2
source share

null simply means that the object (in this case, the textLogin.Text object) does not exist. For this to be the case, the textLogin object cannot exist. Thus, the textLogin object textLogin not null in this case, and therefore textLogin.Text cannot be null.

"" , on the other hand, means an empty string, which means that there is nothing in the text of the text field. those. textLogin.Text does not contain any characters inside it.

+1
source share

In the non-classic term, null means no value and "" middle string is of zero length, which is not the same. There may be some software that processes the string null and "" same way, for example. Console.WriteLine , but it still doesn't make them the same.

Strictly speaking, the expression "" == null is false in design. String equality comparisons in the .NET Framework work with the == operator overload , which does not treat null as "" .

+1
source share

You can use IsNullOrWhiteSpace to verify that the text field is entered correctly. It checks for a null, empty line or space (tab, space, etc.). http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx

0
source share

Simple, "" has a valid value, i.e. String.Empty , but null has no meaning.

0
source share

The difference is that "" means an empty string, but null means that it does not exist

0
source share

All Articles