Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
TextBox2.Text =TextBox3.Text = "" ;
what is difference between this coding 
TextBox2.Text ="";
TextBox3.Text = "";

which is best

What I have tried:

<pre>
TextBox2.Text =TextBox3.Text = "" ;
what is difference between this coding 
TextBox2.Text ="";
TextBox3.Text = "";

which is best
Posted
Updated 4-Jun-19 2:51am
Comments
F-ES Sitecore 4-Jun-19 8:58am    
Using string.Empty rather than "" is even better :)
NithyaKumarJai 4-Jun-19 9:38am    
Yeah thanks, i know that process i want conclusion for my question

1 solution

There is no functional difference between the two. a = b = ""; is equivalent to a = ""; b = "";. *

There is no "best": you should generally go for the most readable and adaptable code, but in this case neither is really better or worse, in my opinion. Go with what you prefer.

* A little bit more in depth: a = b = ""; is parsed as a = (b = ""); which means: b gets set to "", the expression (b = "") returns "" (because that's the new value of b) and a gets assigned that value.
 
Share this answer
 
v4
Comments
NithyaKumarJai 4-Jun-19 9:35am    
thanks Thomas Daniels
Ravi Bhavnani 4-Jun-19 16:11pm    
Actually there is a difference in performance when it comes to non-primitive types. Chaining assignments is not recommended when the value accessor performs an expensive (or non-zero effort) operation. For this reason (in the case of what the OP asked), it's better to do:

TextBox2.Text = string.Empty;
TextBox3.Text = string.Empty;

rather than TextBox2.Text = TextBox3.Text = string.Empty;

/ravi
Thomas Daniels 4-Jun-19 16:21pm    
Good point, didn't think of that. I updated my answer to quote you.
Richard Deeming 5-Jun-19 20:06pm    
Are you sure about that?

a.Text = b.Text = Bar(); calls Bar exactly once, and the set accessor of the properties exactly once per instance. The get accessor is never called.

Example[^]
Ravi Bhavnani 5-Jun-19 22:10pm    
My point was that in the case of the OP's question, it's cheaper to initialize the Text property of the 2 controls to string.Empty rather than getting the Text property of the first one in order to use as the value of the 2nd control. Unless I'm mistaken, the Text accessor calls SendMessage (hWnd, WM_GETTEXT) under the covers. It would be cheaper to instead use the constant string.Empty.

/ravi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900