Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
C# Nullable Static Analysis—Can you use conditional post-conditions with multiple return values?

Using C# 8+ with nullable context enabled, I have a method which returns

  1. An enum representing various error codes, or success;
  2. An object (if success) or null (if error)

as a ValueTuple. Aside from the null-forgiving operator, is there a way I can tell the compiler that the object is not null if the enum value indicates success?

private (EnumResult, SomeClass?) DoThing(...)
{
    if (...)
        return (EnumResult.Error1, null);

    if (...)
        return (EnumResult.Error2, null);

    return (EnumResult.Success, new SomeClass(...));
}
(EnumResult result, SomeClass? someClass) = DoThing(...);

if (result == EnumResult.Success)
{
    // `someClass` should not be null here, but the compiler doesn't know that.
}

I am aware there are nullable static analysis attributes which can be applied when using a bool return and an out parameter:

private bool TryDoThing(..., [NotNullWhen(true)] out SomeClass? someClass)
{
    if (...)
    {
        someClass = null;
        return false;
    }

    someClass = new SomeClass(...);
    return true;
}
if (TryDoThing(..., out SomeClass someClass))
{
    // The compiler knows that `someClass` is not null here.
}

But I couldn't determine how to apply something similar when returning a ValueTuple.

I am using an enum rather than a bool or throwing exceptions because the result code is communicated across a named pipe, where the process on the other side parses it back into an enum and then reacts according to the specific error. I am using a ValueTuple rather than an out parameter out of personal preference.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No. At this time, the MaybeNullWhen/NotNullWhen attributes only work to signal that the null state of an out parameter depends on a bool return value.

There is currently no plan to allow the null state of a variable to depend on the value of an enum return value, for example.

There is also no plan to allow an interdependence between the elements of a tuple return value, i.e. the (object? result, bool ok) Method() pattern. If you are interested in such functionality getting added to the language, feel free to start a discussion on how it would work at https://github.com/dotnet/csharplang/discussions.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!