Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda