Question: I am running into this situation again and again, I want a function to return both a named ValueTuple and a success flag

All the options I can think of are not great :(

**Option1:**

```
bool MyFunc(T1 param, out (Type1 name1, Type2 name2)? result)

if (!MyFunc(param, out var result)) { .. .. my error handling }

.. do stuff with `result.Value.name1` and `result.Value.name2`

```

Ugly with needing to define result and needing `result.Value` everywhere

**Option2:**

```
bool MyFunc(T1 param, out (Type1 name1, Type2 name2)? result)

if (!MyFunc(param, out var result)) { .. .. my error handling }
var (name1, name2) = result.Value;

.. do stuff with name1, name2

```

Ugly with needing to define result and to explicitly destruct the tuple

**Option 3**:

```
(Type1 name1, Type2 name2)? MyFunc(Type3 param) {}

var myFuncRes = myFunc(param);
if (myFuncRes is null) { .. my error handling }

.. do stuff with myFuncRes.Value.name1 etc 

```

Ugly with needing `myFuncRes` and `.Value` or explicitly destructing the tuple

What I would like to be able to do is something like:

```
(Type1 name1, Type2 name2)? MyFunc(T1 param)

if (var myFuncRes = MyFunc(param) is null) { .. .. my error handling }

.. do stuff with `myFuncRes.name1` and `myFuncRes.name2` directly !

```

This is my explicit intention .. but this is not allowed 

Are there better ways or common solutions ?

(Note I am not interested in throwing exceptions here)

Answer: 
You can do:

```
(Type1 name1, Type2 name2)? MyFunc() {...} 

if (!(MyFunc() is {} myFuncRes))
{
    // Error handling
    return;
}
// use myFuncRes.name1 and myFuncRes.name2

```

This uses [pattern matching](https://learn.microsoft.com/en-us/dotnet/csharp/pattern-matching). `{}` is a property pattern which doesn't actually specify any properties, and so it matches any object which is not `null`. [See this answer](https://stackoverflow.com/questions/3982658/c-sharp-object-objs-value-is-what-is). 

It's not hugely readable, though, IMO. There are discussions about adding new patterns to improve cases like this, such as `is not null`.

There's also [a proposal](https://github.com/dotnet/csharplang/issues/611) to improve the case of destructuring a tuple in an out declaration, which would let you write:

```
bool MyFunc(out (Type1 name1, Type2 name2) result) {...} 

if (!MyFunc(out var (name1, name2)) 
{

} 

```
This approach could work if the tuple is not supposed to be used for anything if the return value is false. When that happens, we just provide a default tuple (which should be promptly ignored) rather than trying to communicate that it is null via a nullable type.

```
static bool Test(T1 param, out (Type1 name1, Type2 name2) result)
{
    try
    {
        //Successful case
        result = ( X, Y );
        return true;
    }
    catch
    {
        //Failure case
        result = (default(Type1), default(Type1));
        return false;
    }
}

```

Now all the caller needs to write is:

```
if (Test(param, out var result))
{
    Console.WriteLine("Your results were {0} and {1}", result.name1, result.name2);
}

```