-
Consider this, in C#9 with .NET 5
Expected: x of type string, not inferred as nullable, and PropertyName is the new PropertyName property in NonemptyPropertyChangedEventArgs. Actual: x of type string?, i.e. inferred as nullable, and PropertyName is the base.PropertyName property which is string? in PropertyChangedEventArgs. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
This is by design of Examine this code: var x = new XXX();
x = null; To keep compatibility in this scenario, |
Beta Was this translation helpful? Give feedback.
-
Note that your If you make it public, then the nullability seems to work fine. This doesn't throw a warning: public class C
{
public void M(NonemptyPropertyChangedEventArgs e)
{
Console.WriteLine(e.PropertyName.Length);
}
}
public class NonemptyPropertyChangedEventArgs : PropertyChangedEventArgs
{
public NonemptyPropertyChangedEventArgs(string propertyName) : base(propertyName)
{
}
public new string PropertyName => base.PropertyName ?? throw new Exception();
} Dotnetfiddle (I'm not using SharpLab, as it doesn't seem to be referencing .NET 5, which is where Whereas if you comment out This also explains why Go To Definition didn't appear to work -- you were actually referencing |
Beta Was this translation helpful? Give feedback.
Note that your
PropertyName
doesn't have an access modifier, which makes it private.If you make it public, then the nullability seems to work fine. This doesn't throw a warning:
Dotnetfiddle (I'm not using SharpLab, as it doesn't seem to be referencing .NET 5, which is where
PropertyChangedEventArgs
i…