How to return a named tuple with only one field

Viewed 1401

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?

3 Answers

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);
    }
}

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.

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);
Related