C# String.Empty [duplicate] - c#

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the difference between String.Empty and “”
Hello
Simple Question;
Why
Textbox1.Text = String.Empty;
is better than
Textbox1.Text = "";
?

It's not, really.
Basically, decide which you find to be more readable. Personally I use "" instead of string.Empty, but others prefer the latter.
Back in .NET 1.x days apparently there was some tiny performance difference (almost certainly irrelevant in real apps) but I believe these days even that's gone.
Use whichever you and your find most readable.

Because String.Empty is actually defined as ""
Best practices dictate not to use string literals but constants.
On a funnier note: String.Empty is/looks more Object Oriented-ISH.

Related

Why string.format? [duplicate]

This question already has answers here:
String output: format or concat in C#?
(32 answers)
Why use String.Format? [duplicate]
(7 answers)
Closed 9 years ago.
Why shouldn't we simply use
string s=product.Name+" has been saved";
instead of:
string s=string.Format("{0} has been saved", product.Name);
One naive reason would be that it helps to prevent exactly the string formatting issue that you've presented in your original (unedited) question i.e.
string s=product.Name+"has been saved";
requires an extra space. The format method aids readability.
You could do that, no one say that you cannot. But mainly for readability, the second approach is prefered. It's even more obvious as soon as you concat more than 2 strings, it gets really messy, hard to read and mantain.
If you have many strings that you want to add, each + operation create new string.
For adding many strings you can use StringBuilder Class or String.Format

Why is string.Empty readonly? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why isn't String.Empty a constant?
...and not const?
I know its probably a useless question, but I'm sort of curious on the reasoning behind this one. An empty string is an empty string so I do not foresee many chances of string.Empty being anything else than "". So why make it readonly?
Is there any other benefit I'm missing in not making it const?
It IS static.
public static readonly string Empty;
Readonly prevents it from being changed. Perhaps you mean "why isn't it a const"?

Benefits of using string.Empty [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between String.Empty and “”
Why it is recommended to use string.Empty instead of ""? From one side, I understand, that it makes code clearer, without "Magic constants". Is there any another technical benefits of using string.Empty?
Thanks.
Per Brad Abrams blog:
the difference between String.Empty and “” are pretty small, but there
is a difference. “” actually creates an object, it will likely be
pulled out of the string intern pool, but still… while String.Empty
creates no object… so if you are really looking for ultimately in
memory efficiency, I suggest String.Empty. However, you should keep
in mind the difference is so trival you will like never see it in your
code…
It's pretty insignificant.
I believe this has been answered before, but to summarize: preference and readability.
They are completely equivalent, and are up to whatever you and your fellow developers prefer, just be consistent. :)

The benefits of using String.Empty [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C#, should I use string.Empty or String.Empty or “” ?
I just don't understand the benefits of using String.Empty over "". Aside it being strongly typed its longer.
So please, why should I be using this?
As has been mentioned string.Empty creates no object where as "" will create an object (albeit pulled out of the pool). The performance difference is minimal but the question should be what is more readable. I take the stance that string.Empty sticks out more than "" and I can clearly see the intent of the declaration (string.Empty vs null). Regardless of your choice you should be consistent throughout your code base.
String.Empty vs ""
There is no meaningful difference. See these (and other) questions elsewhere on SO:
In C#, should I use string.Empty or String.Empty or "" to intitialize a string?
Why is string.Empty more efficient than "" in .Net
String.Empty versus ""
What is the difference between String.Empty and “” and null?
Which should I use for empty string and why?
Difference between string.Empty and ""

Difference between string.Empty and "" [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C#, should I use string.Empty or String.Empty or “” ?
Is there any difference in c# between the following declarations...
private string m_port = string.Empty;
or...
private string m_port = "";
Is it just coding standard that makes it look neater?
Just coding standard...
http://msdn.microsoft.com/en-US/library/system.string.empty%28VS.80%29.aspx
They are the same, but looks better.
The main difference is one of semantics - string.Emtpy says that you meant to have an empty string. "" might be a mistake (" ", for instance).
Since String.Empty is an instance it should remove the overhead of the object creation, however I think a parser should be smart enough to figure this out.
All string literals is also pulled from an object pool I believe, so the object creation might not even take place.

Categories