'C# Nullable Annotation that method returns not null if parameter is not null
How can I tell compiler that the following extension method returns not null if input is not null?
public static string? SomeMethod(this string? input)
{
if (string.IsNullOrEmpty(input))
return input;
// Do some work on non-empty input
return input.Replace(" ", "");
}
Solution 1:[1]
Use the following attribute:
[return: NotNullIfNotNull("input")]
public static string? SomeMethod(this string? input)
{
...
}
For further reading: Attributes for null-state static analysis interpreted by the C# compiler
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Svyatoslav Danyliv |
