Difference between string.Empty and "" [duplicate] - c#

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.

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

How many string additions do you need for a StringBuilder to be more efficient? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Stringbuilder vs String.Concat
As I understand it, String are immutable, so if you do "one " + "two", rather than reusing the memory of these two strings, a new string is created with value "one two". This becomes a problem if you do something like:
String output = "";
for (int i = 0; i < 20; i++){
output = output + ii.toString();
}
Because 19 (or 39?) strings are created and discarded to produce the final desired output.
String builders are mutable, so you can add strings together efficiently, and then convert them into a string when you are done.
I assume there is some overhead in creating string builders, and also the clever compilers used today optimize away lots of string addition problems. So my question is: how many strings do I need to add together before it becomes more efficient to use a StringBuilder instead? Or are my assumptions incorrect?
An answer in this question - Does StringBuilder use more memory than String concatenation? - states 'a few dozen' is the threshold.
And linked from another answer in that question is this link - http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html - which concludes with "it doesn't matter"!

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. :)

C# String.Empty [duplicate]

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.

Categories