Is there any need to set a string member variable of a class to ""? Will it be 开发者_运维知识库null
unless you do this?
The reason a string will be null is because a string is a reference type and the default value for all reference types is null (i.e. no reference).
The best reference is the Default Values Table (remember all reference types are set to null by default) but some examples below give a decent idea:
string a = default(string); // null
String b = default(String); // null
int c = default(int); // 0
int? d = default(int?); // null
Object e = default(object); // null
double f = default(double); // 0.0
// an instance of someStruct with fields set to default(theirType)
someStruct g = default(someStruct);
A string kind of seems to be a value type because it seems so primitive. Additionally it is immutable meaning that you can never modify a string, instead you just set string symbols to point to new string "values" (which are themselves references), giving the impression that string is a value type.
For example:
string a = "boo";
/* Mem contains reference to "boo" and symbol 'a' => reference("boo") */
a = "gah";
/* Mem contains references to "boo" and "gah". symbol 'a' -> reference("gah") */
For more info on strings see this great article.
And don't forget the Default Values Table.
Yes, the default value of a string is null.
A string's default value is null.
This has already been asked and answered many times. And the previous answerers here have jumped straight to your second question without answering your first, which I feel is the more important since a simple Google search would have told you what the default is.
"Is there any need to set a string member variable of a class to ""?"
It depends on what you mean by "need". If you are into readability then yes, there could very well be a need. It will be a personal decision that requires a consensus decision by your team. I personally find things much more readable if variables are given a default value in an appropriate place. If you do it in the right place the compiler will (can) optimize it away, so it really comes down to readability.
Some excellent insight can be found here.
- Is there any need to set a string member variable of a class to ""?
Not if your class methods know that the default value of a string member variable is null
and deal with it accordingly (i.e., check it for null before using its value).
Otherwise, it is probably a good idea to initialize it to some non-null value. This can simplify your methods if they can assume that the variable has a non-null value at all times.
- Will it be null unless you do this?
Yes, a member variable of reference type (e.g., string
) without an explicit initializer value is initialized to null
by default when the class object is constructed.
Also, if your app does not distinguish between null and empty "", then when you test the value, use String.IsNullOrEmpty()
. If you also treat whitespace the same as empty, then use the new String.IsNullOrWhiteSpace()
. null, "", and " " will be true. Any non-whitespace characters will be false.
string s = GetSomeString();
if(String.IsNullOrEmpty(s)) {
//do whatever here
}
精彩评论