I have a function that returns a nullable result and has an out parameter that is also nullable:
public Result? GetResult(out Parameter? parameter)
{
Result? result = //...get the result somehow
if (result is not null)
{
//...do stuff
parameter = something;
return result;
}
parameter = null;
return null;
}
Is there a way to use the attribute NotNullIfNotNull in parameter but regarding whether result is not null?
Something like:
public Result? GetResult([NotNullIfNotNull(return)] out Parameter? parameter)
I want to avoid having to check both result and parameter for null outside the function.
outparameters and returntrueif they're both not null, then you can put[NotNullWhen(true)]on both output parameters. Test the return value and use both with impunity if it's true. Addressing the imbalance of return value vs. output parameter leads to simpler solutions.(Result, Parameter)?, so you return eithernull, or a tuple of two non-null things.(Parameter, Result)and get rid ofout.if. I would do it, but the inquirer asks how to avoidif. I doubt it makes sense and not sure the initial requirement makes sense.