Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda