Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable Reference Types: A warning should be reported when dereferencing a field/property before it is assigned #27016

Closed
PathogenDavid opened this issue May 21, 2018 · 3 comments
Labels
Area-Compilers Bug Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design
Milestone

Comments

@PathogenDavid
Copy link
Contributor

Version Used: 05/14/18 Nullable Reference Types Preview (csc reports 2.8.0.62830 (e595ee27)) with Visual Studio 15.7.1

Demonstration Code:

public class TestClass
{
    private string x;

    public TestClass()
    {
        Console.WriteLine(x.ToString()); // No warning, CS8602 is expected
        x = "Hello, world!";
    }
}

Expected Behavior:

A warning is reported when x is dereferenced.

Actual Behavior:

There is no compile-time warning and a runtime exception occurs when the code is executed.


Additional info:

The current behavior is the same with a property.

@gafter gafter added this to the 16.0 milestone May 31, 2018
@jaredpar jaredpar added the Bug label Aug 30, 2018
@gafter gafter modified the milestones: 16.0, 16.1 Feb 26, 2019
@jaredpar
Copy link
Member

jaredpar commented Apr 9, 2019

Thanks for reporting. At this point the behavior is by design. The field is declared as non-nullable hence the compiler treats it that way. In this example yes it's easy but in other examples, base constructors, chained constructors, the analysis can't be done as easily.

@jaredpar jaredpar closed this as completed Apr 9, 2019
@jaredpar
Copy link
Member

jaredpar commented Apr 9, 2019

@agocke for the greater ctor tracking issues

@jaredpar jaredpar added the Resolution-By Design The behavior reported in the issue matches the current design label Apr 9, 2019
@YairHalberstadt
Copy link
Contributor

YairHalberstadt commented Sep 5, 2019

I find this a really common source of bugs.

If a constructor does not call a base constructor, or a this constructor, I believe all fields will always be null, unless they are initialized at the declaration site.

So a simple rule would be that in such a case, all fields start off with a nullable state of maybe null, no matter their annotation, unless they are initialized at the declaration site.

I think this shouldn't apply to properties - just to fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

5 participants