Check this code..
string str;
if (str.Contains("something.."))
{
}
Compiler throws this error for this code
Use of unassigned local variable 'str'
Why does a reference type is not initialized to null ?
Just want to know out of curiosity.
I'd also want to know what happens for code below. how does this assignment work ?
string str = null;
A reference isn't supposed to be null. The variable must be initialized to a non-null value. The variable can never be assigned the value null .
The default value of a reference type is null . It means that if a reference type is a static class member or an instance field and not assigned an initial value explicitly, it will be initialized automatically and assigned the value of null .
Although using nullable reference types can introduce its own set of problems, I still think it's beneficial because it helps you find potential bugs and allows you to better express your intent in the code. For new projects, I would recommend you enable the feature and do your best to write code without warnings.
The C# langauge requires that all variables be definitely assigned to before they are read from. Local variables are considered to be initially unassigned, whereas fields, array elements, and so on, are considered to be initially assigned to their default value. (Which, for a reference type, is null.)
There is no technical reason why we couldn't treat local variables as initially assigned to their default values and throw away all the definite assignment checking. It's there because using an unassigned local as its default value is (1) a bad coding practice, and (2) a highly probable source of irritating bugs. By requiring you to explicitly assign local variables before they are used, we prevent the user from using a bad practice, and eliminate a whole class of bugs that you then never have to debug.
Also, consider the following:
while(whatever)
{
int i;
print(i);
i = i + 1;
}
Do you expect i to hold its value across executions of the loop, or for it to be initialized fresh to zero every time? By forcing you to explicitly initialize it, the question becomes meaningless and this is a difference which makes no difference.
(Also, in the case above there is a small potential performance optimization, in that the compiler could re-use the variable without having to generate code to clear its contents because the compiler knows that you will clear the contents.)
I don't know how to answer your second question because I don't know what you mean by "work". Can you tell me how assigning "int x = 123;" works? Once I know what you mean by "works" then I can describe how assigning a null to a variable of reference type works.
Only fields (variables declared at class level) are initialized automatically:
null
reference. What you are declaring is a "local variable" inside a method. Local variables are not automatically initialized regardless of being a value type or reference type.
I'd also want to know what happens for code below. how does this assignment work ?
This assignment initializes the local variable with null value with an ldnull
instruction followed by a stloc
instruction (in case it's not optimized out, of course) and more importantly, satisfies the compiler's data flow analysis rules for definite assignment. C# specification defines a concept called definite assignment that ensures a variable is assigned before first use.
It does get initialized to null. The compiler just did you a favor by not having to debug the inevitable NullReferenceException you'll get.
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