How to return a named tuple with only one field

10

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?

Share
Improve this question
9
  • 1
    what exactly is your use for a one-element named tuple? – Nailuj29 Apr 8 at 13:25
  • 4
    This sounds like an opportunity for a out parameter instead... – Sweeper Apr 8 at 13:25
  • 1
    You could just make it a regular function and return a bool instead of a tuple, and add XML documentation comments to the code describing what the return value represents. – mason Apr 8 at 13:27
  • 1
    Although .NET allows for single-element tuples, C# does not, as there's no way to make the syntax unambiguous and the usefulness is dubious. – Jeroen Mostert Apr 8 at 13:27
  • 1
    One option is to create a DoSomethingResult class or struct and return that instead and then just update it whenever you need to return more values. You can even add deconstruction to it so that it will be similar to a value tuple. – juharr Apr 8 at 13:36

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue