Possible Duplicate:
What is the difference between String.Empty and “”
Is ""
equivalent to String.Empty
?
Which is preferred for initializing string values?
The main difference between null and empty is that the null is used to refer to nothing while empty is used to refer to a unique string with zero length. A String refers to a sequence of characters.
So, NULL is better. An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.
An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all. A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn't of 0 length.
They are different. A NULL string does not have any value it is an empty char array, that has not been assigned any value. An empty string has one element , the null character, '\0'. That's still a character, and the string has a length of zero, but it's not the same as a null string, which has no characters at all.
public sealed class String {
//...
public static readonly String Empty = "";
//...
}
Use null
when you want to represent that there is no value;
Use String.Empty
when you want to represent that there is a value, but the value is a blank string.
String.Empty because it is a static variable, rather than "" which has to create a new string, and null means that you must then set the string equal to a new instance of a string.
(thanks for the correction)
Yes. And String.Empty, but please don't worry about it.
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 and String.Empty creates no object, so if you are really looking for ultimate 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…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With