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.
txtUserName.Clear();
This code clears the textbox. It will set the Textbox value to ""
txtUserName.Text = string.Empty;
Doesn't create object. This executes faster than txtUserName.Text = ""
;
txtUserName.Text = "";
Creates object and affects the performance.
""
creates an object while String.Empty
creates no object. So it is more efficient to use String.Empty.
Refference: String.Empty vs ""
Regarding .Clear()
i didnot get better answer then @syned's answer.
The Clear()
method does more than just remove the text from the TextBox
. It deletes all content and resets the text selection and caret as @syned's answer nicely shows.
For the txtUserName.Text = "";
example, the Framework will create an empty string
object if one does not already exist in the string pool and set it to the Text
property. However, if the string ""
has been used already in the application, then the Framework will use this value from the pool.
For the txtUserName.Text = string.Empty;
example, the Framework will not create an empty string
object, instead referring to an empty string constant, and set this to the Text
property.
In performance tests, it has been shown (in the In C#, should I use string.Empty or String.Empty or “”? post) that there really is no useful difference between the latter two examples. Calling the Clear()
method is definitely the slowest, but that is clearly because it has other work to do as well as clearing the text. Even so, the difference in performance between the three options is still virtually unnoticeable.
If you are behind some performance differences or memory leaks, there are not much (just some additional calls to events when setting text instead of using .Clear() )
However, you dont have access to control itself when using MVVM, so only way to clear the text is by setting text to binded property with TextBox.
In standard application, you can do whatever you want (I will prefer using .Clear() method which is designed for this purpose).
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
The string.Empty field is an empty string literal. It is slightly different from an empty string literal constant " ". There is a subtle difference—but one that could be significant in some scenarios. It changes the meaning of a program
We use string.Empty and "" in a C# program. The string.Empty field is initialized to "" at runtime by the .NET Framework.
You cannot use string.Empty as a switch case, because it cannot be determined at compile-time by the C# compiler.
Which explain the differences of string.empty and ""
The Clear()
method does more than just remove the text from the TextBox
. It deletes all content and resets the text selection