What is the difference between these three ways to clear a Textbox?

前端 未结 10 655
孤城傲影
孤城傲影 2021-02-01 17:11

I am bit confused between the below three ways to clear the contents of a textbox. I am working with WPF and found All are working, but I am unable to find the difference.

10条回答
  •  萌比男神i
    2021-02-01 17:31

    Let's go through the commands one by one.

    txtUserName.Clear();
    

    The Clear() command assigns the texbox an empty string just like the next example. Source (best explination is given by syned on this point)

    txtUserName.Text = string.Empty;
    

    Now the string.Empty the actuall code for it is

    static String()
    {
        Empty = "";
    }
    

    Meaning that you assign the string "" after compile time.

    txtUserName.Text = "";
    

    Now here you just assign the "" string directly to the object on compile time.

    Small side note txtUserName.Text = ""; is faster than txtUserName.Text = string.Empty; Source

提交回复
热议问题