How to avoid irrelevant nullable warning (without explicit suppression)
Will be be able to use the MemberNotNullWhen
attribute in C# 9 (currently in preview):
[MemberNotNullWhen(true, "Bar")]
bool GenerateArray => Bar.HasValue;
The relevant attribute types will exist in .Net 5:
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
public sealed class MemberNotNullAttribute : Attribute
{
public MemberNotNullAttribute(params string[] members) { }
public MemberNotNullAttribute(string member) { }
}
}
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
public sealed class MemberNotNullWhenAttribute : Attribute
{
public MemberNotNullWhenAttribute(bool when, params string[] members) { }
public MemberNotNullWhenAttribute(bool when, string member) { }
}
}
Illustration on sharplab