Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 4.7 returning Tuples and nullable values

Ok lets say I have this simple program in .NET 4.6:

using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static async void Main()
        {
            var data = await Task.Run(() =>
            {
                try
                {
                    return GetResults();
                }
                catch
                {
                    return null;
                }
            });

            Console.WriteLine(data);
        }

        private static Tuple<int,int> GetResults()
        {
            return new Tuple<int,int>(1,1);
        }
    }
}

Works fine. So with .NET 4.7 we have the new Tuple value type. So if I convert this it becomes:

using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static async void Main()
        {
            var data = await Task.Run(() =>
            {
                try
                {
                    return GetResults();
                }
                catch
                {
                    return null;
                }
            });

            Console.WriteLine(data);
        }

        private static (int,int) GetResults()
        {
            return (1, 2);
        }
    }
}

Great! Except it doesn't work. The new tuple value type is not nullable so this does not even compile.

Anyone find a nice pattern to deal with this situation where you want to pass a value type tuple back but the result could also be null?

like image 388
Kelly Avatar asked May 12 '17 21:05

Kelly


People also ask

Can tuple be null C#?

Comparing null to null The null == null comparison is allowed, and the null literals do not get any type. In tuple equality, this means, (0, null) == (0, null) is also allowed and the null and tuple literals don't get a type either.

Can a tuple have null values?

StreamBase supports the use of null values in applications. Nulls can be used to explicitly represent data that is missing or unknown. Use the reserved value null to indicate that the value of a tuple field is null.

Are tuples immutable C#?

One thing that makes tuples distinct is that they are immutable. That means you can't change them at all once they have been created. Any properties in that tuple are going to be read-only. It also means that the size of the tuple is fixed after you create it.

When should I use tuple C#?

Tuples can be used in the following scenarios: When you want to return multiple values from a method without using ref or out parameters. When you want to pass multiple values to a method through a single parameter. When you want to hold a database record or some values temporarily without creating a separate class.


1 Answers

By adding the nullable type operator ? you can make the return type of the GetResults() function nullable:

private static (int,int)?  GetResults()
{
    return (1, 2);
}

Your code would not compile though because async is not allowed in the Main() function. (Just call another function in Main() instead)


Edit: Since the introduction of C# 7.1 (just a few months after this answer was originally posted), async Main methods are permitted.

like image 165
adjan Avatar answered Oct 05 '22 23:10

adjan