Friday, December 21, 2007

Can TextBox.Text ever be null?

Recently I have been asked this question. Answer is that it cannot be null and will not be null, so you don't have to:
.Text != null

Why? Because .Text property in base class does:
    set 
{
if (value == null)
{
value = "";
}
.....
}

Above is for TextBox from Windows.Forms, as for TextBox from WebControls it's the same. The getter looks like this:
    get 
{
string str = (string) this.ViewState["Text"];
if (str != null)
{
return str;
}
return string.Empty;
}

If you don't believe me just try to assign a null value :-)

No comments: