why are textbox1.text == null and textbox1.text == "" different?

just found out that textbox1.text == null and textbox1.text == "" are different.

I am checking if textbox1.text has value or not, if not, show message. Firstly I tried textbox1.text == null, it didnt work, then changed it to textbox1.text == "", it worked.

why plz help




Answer this question

why are textbox1.text == null and textbox1.text == "" different?

  • Gr1nch

    The string type is a reference type for performance reasons, so that long strings don't have to be allocated on the stack every time they are passed around. As a reference type, string can then have a null value, or a specific value.

    To deal with this, you can use string.IsNullOrEmpty(...) which can test for both conditions at once.

    Thanks,
    Luke Hoban
    Visual C# IDE Program Manager


  • jam201984

    thx luke, it's very clear explanation :)

  • why are textbox1.text == null and textbox1.text == "" different?