dotnet / dotnet/roslyn

Default value of a struct type should give nullable initial state to some field-backed properties

Open
#84,987 2 comments 0 reactions 1 assignee Claimed by @RikkiGibson View on GitHub
Area-Compilers Feature - Nullable Reference Types help wanted
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

The current behavior is dependent on whether we are currently constructing the struct instance, and feels a little inconsistent.

Basically, it feels like we should be able to get a similar behavior for member flow states, when any `default` value of a struct type is used, as when we are working out the initial state of struct fields in a constructor.

Note that 'null-resilient' properties using `field` keyword would be excepted from this. Their initial flow state would be the same as the property. Currently working on a change to make that work more consistently in constructors. (resolving #77991).

I consider this issue low priority

```cs
struct S
{
public void M()
{
this = default;
this.Prop2.ToString(); // no warning
}

public S()
{
this = default;
Prop2.ToString(); // warning
}

public string Prop2 { get; set; }
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.