C# Nullable Annotation that method returns not null if parameter is not null
C# Nullable Annotation that method returns not null if parameter is not null
如果输入不为空,我如何告诉编译器以下扩展方法 returns 不为空?
public static string? SomeMethod(this string? input)
{
if (string.IsNullOrEmpty(input))
return input;
// Do some work on non-empty input
return input.Replace(" ", "");
}
使用以下属性:
[return: NotNullIfNotNull("input")]
public static string? SomeMethod(this string? input)
{
...
}
进一步阅读:Attributes for null-state static analysis interpreted by the C# compiler
如果输入不为空,我如何告诉编译器以下扩展方法 returns 不为空?
public static string? SomeMethod(this string? input)
{
if (string.IsNullOrEmpty(input))
return input;
// Do some work on non-empty input
return input.Replace(" ", "");
}
使用以下属性:
[return: NotNullIfNotNull("input")]
public static string? SomeMethod(this string? input)
{
...
}
进一步阅读:Attributes for null-state static analysis interpreted by the C# compiler