Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuple.Create : The type arguments for method cannot be inferred from the usage

Tags:

c#

tuples

I'm trying To make the method to return a Tuple of: Tuple<DateTime?, DateTime?>.

In case the code is being able to generate two DateTime types, I'm uses the Tuple.Create to create a return statement with:

public Tuple<DateTime?, DateTime?> GeTuple()
{
    if (something)
    {
        return Tuple.Create(startDate, endDate);
    }
    else
    {
        return Tuple.Create(null, null); //--> This line produce the error.
    }
}

but I'm getting this error of:

The type arguments for method 'Tuple.Create(T1, T2)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

like image 385
Shahar Shokrani Avatar asked Sep 09 '26 10:09

Shahar Shokrani


1 Answers

The compiler can't look at Tuple.Create(null, null) and tell that you meant to create a Tuple<DateTime?, DateTime?>. The fact that it's then being returned from a method which returns a Tuple<DateTime?, DateTime?> doesn't matter: the compiler doesn't consider that.

Use one of these:

new Tuple<DateTime?, DateTime?>(null, null)

Tuple.Create((DateTime?)null, (DateTime?)null)
like image 96
canton7 Avatar answered Sep 11 '26 00:09

canton7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!