首页 > 解决方案 > 函数返回后是否可以将参数的属性标记为空/非空?

问题描述

如果我有一个 function bool Foo(string? input),那么我可以对其进行注释以表明如果 return 是truetheninput不为 null:

public static bool Foo([NotNullWhen(true)] string? input)

是否可以对参数的属性执行此操作(或类似的操作)?我在想一些事情

public class AClass { string? NullableString {get;set;}}
public static bool Foo([PropertyNotNullWhen(true, nameof(AClass.NullableString))] AClass input)

我认为MemberNotNullWhen在这种情况下不会起作用,因为它仅适用于方法、属性和索引器,而不适用于它们的参数。

标签: c#annotationsnullable-reference-typesc#-9.0

解决方案


推荐阅读