CommunityToolkit / CommunityToolkit/dotnet
ObserveableProperty and Nullable Can Generate Warnings
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### Describe the bug
An observable property in a nullable class like the one shown here can cause somewhat intractable warnings.
``` CSharp
using CommunityToolkit.Mvvm.ComponentModel;
using System.Diagnostics.CodeAnalysis;
namespace ObservableTest;
internal partial class TestViewModel:ObservableObject
{
TestViewModel()
{
Helper();
}
[MemberNotNull(nameof(testString))]
private void Helper()
{
TestString = string.Empty;
}
[ObservableProperty]
private string testString;
}
```
### Regression
_No response_
### Steps to reproduce
```text
Create a default MAUI app and add a class like the one shown above.
Observe that the line:
[MemberNotNull(nameof(testString))]
Gets a warning for referring to testString, not TestString, but referring to TestString generates a different warning (CS8618: Non-nullable field 'testString' must contain a non-null value when exiting constructor)
```
### Expected behavior
Either don't warn on this use of testString or allow TestString to be used here.
### Screenshots
_No response_
### IDE and version
VS 2022 Preview
### IDE version
17.10.0 Preview 1
### Nuget packages
- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)
### Nuget package version(s)
8.2.2
### Additional context
_No response_
### Help us help you
No, just wanted to report this
Contributor guide
Assessment
This issue has not been assessed yet.