Is there any need to set a string member variable of a class to “”? Will it be null unless you do this?
Is there any need to set a string member variable of a class to
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
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:
For more info on strings see this great article.
And don’t forget the Default Values Table.