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

258
Vistas
How to return a named tuple with only one field

I wrote a function in c# which initially returned a named tuple. But now, I only need one field of this tuple and I would like to keep the name because it helps me to understand my code.

private static (bool informationAboutTheExecution, bool field2thatIdontNeedAnymore) doSomething() {
        // do something
        return (true, false);
    }

This function compile. But It's the following function that I want

private static (bool informationAboutTheExecution) doSomething() {
        // do something
        return (true);
    }

the error messages:

Tuple must containt at least two elements

cannot implcitly convvert type 'bool' to '(informationAboutTheExecution,?)

Has somebody a solution to keep the name of the returned value?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You cannot, basically. You can return a ValueTuple<bool>, but that doesn't have names. You can't add [return:TupleElementNamesAttribute] manually, as the compiler explicitly does not let you (CS8138). You could just return bool. You can do the following, but it isn't any more helpful than just returning bool:

    private static ValueTuple<bool> doSomething()
        => new ValueTuple<bool>(true);

Part of the problem is that ({some expression}) is already a valid expression before value-tuple syntax was introduced, which is why

    private static ValueTuple<bool> doSomething()
        => (true);

is not allowed.

over 4 years ago · Santiago Trujillo Denunciar

0

If you must name your return, you can do this:

private static void doSomething(out bool information) {
    // do something
    information = true;
}

then call it with

bool result;
doSomething(out result);
over 4 years ago · Santiago Trujillo Denunciar

0

I just want to add another option, althought he out is the easiest workaround and Marc explained already why it's not possible. I would simply create a class for it:

public class ExecutionResult
{
    public bool InformationAboutTheExecution { get; set; }
}

private static ExecutionResult DoSomething()
{
    // do something
    return new ExecutionResult{ InformationAboutTheExecution = true };
}

The class can be extended easily and you could also ensure that it's never null and can be created with factory methods like these for example:

public class SuccessfulExecution: ExecutionResult
{
    public static ExecutionResult Create() => new ExecutionResult{ InformationAboutTheExecution = true };
}
public class FailedExecution : ExecutionResult
{
    public static ExecutionResult Create() => new ExecutionResult { InformationAboutTheExecution = false };
}

Now you can write code like this:

private static ExecutionResult DoSomething()
{
    // do something
    return SuccessfulExecution.Create();
}

and in case of an error(for example) you can add a ErrorMesage property:

private static ExecutionResult DoSomething()
{
    try
    {
        // do something
        return SuccessfulExecution.Create();
    }
    catch(Exception ex)
    {
        // build your error-message here and log it also
        return FailedExecution.Create(errorMessage);
    }
}
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