When using "Implement interface" quick action for INotifyPropertyChanged interface I am getting
class Test : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
}
The nullable reference type is expected, this force me to use ?.Invoke()
rather than getting NullReferenceException
.
However for IValueConverter it's:
class Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
I expect instead to see object?
everywhere for method arguments and return types. Why is it not?
Should I manually do this change?
Another thing I noticed:
interface IA
{
object? Test(object? arg);
}
class A : IA
{
public object? Test(object? arg)
{
throw new NotImplementedException();
}
}
interface IB
{
object Test(object arg);
}
class B : IB
{
public object Test(object arg)
{
throw new NotImplementedException();
}
}
Means the "Implement interface" quick action simply follows the method signatures from interface definition. Does this means that the signature of all standard library interfaces needs to be updated?
I am using VS2022 preview 4 and .Net 6.0 btw.
CodePudding user response:
Does this means that the signature of all standard library interfaces needs to be updated?
Yes, it does.
From Microsoft's documentation:
The .NET runtime APIs have all been annotated [for null-state static analysis] in .NET 5. You improve the static analysis by annotating your APIs to provide semantic information about the null-state of arguments and return values.
This is why you're seeing PropertyChangedEventHandler?
for INotifyPropertyChanged
; it's been updated to include said annotations.
However, the most recent version of the IValueConverterInterface
is Windows Desktop 5. From that page, you can see that it has not yet been updated to take advantage of nullable reference types.