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.
Can someone please explain to me about it with some examples?
txtUserName.Clear();
txtUserName.Text = string.Empty;
txtUserName.Text = "";
If not going really deep:
Clear: remove content from TextBox and may be delete resources allocated with it
Assigning empty string (because string.Empty and "" are equal) to Text property just assign empty string to attached property TextBox.TextProperty:
It appears that it is doing quite some additional stuff, like checking for the origin of the change, bindings, updating caret position and updating/clearing undo. Most of which is likely not needed when assigning an empty string.
Well.. 1st a warning, this answer have that potential to be outside of the current consensus of most developers here but here goes :) try to read it till the end.
These two (and even additional one I included) are exactly the same:
Even if the assembly in debug configuration might come out a bit different, I am positive that in the release mode which is more optimized they will compile to the exact same assembly.
In case they are not coming out the same - this implies a degraded ability of the complier to translate in the most optimal translation the code agenda of this case, or in other words.. in other languages this might come as the same assembly and from an academic vision of it - it should come out as the same assembly. but not every complier care that much of the academic view of things :)
Regarding the third dude
txtUserName.Clear()
this is a different case, I assume just like you that the inner implementation of this method actually do or simply use one of those three assignments..(and as others already mentioned it do even more beyond just removing the chars from the text)
However, if you think object oriented - assume someone want to create a special textbox which includes for example more things to clear in it - for him it will be very convenient to have that 'Clear' method to override.. and if you used the clear method - you don't change your code when you change from the basic textbox to that new custom/special texbox.
So to sum it up - if you are to use the control, you should use its methods and that means using that 'Clear()' method will be more suitable when you want to clear it especially if one day in the future you would want to replace that textbox with a custom textbox of your own. so at least grammatically it is the better choice..
But yes, it will inflict performance if all you wanted is simply to remove the characters from the text property..
Here is a small program to test the efficiency of each under WPF.
Those were the results I got: 43, 40, 73, 443
And it is consistent - the first two are about the same +/- a mini-second or two, the third is always slightly longer and the last one is definitely longer than all others.
I think that that is as deep as it gets :)
The
Clear()
method does more than just remove the text from theTextBox
. 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 emptystring
object if one does not already exist in the string pool and set it to theText
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 emptystring
object, instead referring to an empty string constant, and set this to theText
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).
This code clears the textbox. It will set the Textbox value to ""
Doesn't create object. This executes faster than
txtUserName.Text = ""
;Creates object and affects the performance.