Auto-assign null or Implicitly Consider nullable references to have null values #4506
Replies: 1 comment 8 replies
-
The purpose of the error is to make sure that you assign the variable in all code paths. This is particularly helpful when you set a variable separately in both branches of an Removing this error would make some cases slightly shorter (but less explicit), but make a certain class of errors more likely. It's not obvious this is a net positive tradeoff. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
With recent versions of C# we generally use null-able local and global declarations of several references.
The compiler is currently taking the unassigned declarations of null-able references as error statements. Because we are already declaring the reference as null-able explicitly, I expect the compiler or the runtime to implicitly assign the null value or consider it as a null value reference.
Declaring a reference as null-able and assigning a null value adds redundant meaning to the declaration. As explicitly declared null-able references are preferred, it's better to have the null assignment removed (as developer's task).
Overall, if a null-able reference declaration is present, then it should either have an implicitly assigned null value or the runtime needs to consider the reference to have null value, rather than the developer explicitly assigning a null value.
Code Example (Current version): This triggers a compiler error - Use of unassigned local variable 'innerException
It would be good to consider the above declaration as a valid statement.
Adding below statement to not to get confused for methods/functions.
If a method/function has any null-able parameter(s), then it doesn't mean that the caller can skip the parameter unless it's default value is declared.
Beta Was this translation helpful? Give feedback.
All reactions