The message warning CS8604: Possible null reference argument is issued on the call to Process(). A way to fix is to mark it with MemberNotNull.
If there is no override in the Derived class then MemberNotNull works as expected.
Why does the compiler still warn if an override exists?
What would be a better way to suppress this warning?
using System;
using System.Diagnostics.CodeAnalysis;
namespace Tests
{
class Base
{
public virtual string? Member { get; set; }
[MemberNotNull(nameof(Member))]
public void Validate()
{
if (Member is null)
{
throw new Exception("Prop is null");
}
}
}
class Derived : Base
{
public override string? Member { get; set; } // warning CS8604: Possible null reference argument
}
[TestClass]
public class MemberNotNullTests
{
static void Process(string s)
{
Console.WriteLine(s);
}
[TestMethod]
public void TestMemberNotNull()
{
Derived instance = new();
instance.Validate();
Process(instance.Member);
}
}
}```