Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting a ValueTuple from a cached object

Tags:

c#

valuetuple

When I am checking my cache to see if if my ValueTuple is in the cache, I use the code below. In this scenario, the value coming back is null (aka does not exist in the cache). When the code runs I get object not set to instance of an object error on the 1st line. Is there a proper way to cast an object to a ValueTuple?

var geo = ((double, double))CacheEngine.Get(key);
if (!geo.Equals(default(ValueTuple<double, double>))) return geo;
like image 800
Chris Auer Avatar asked Dec 24 '22 09:12

Chris Auer


1 Answers

I found that I needed to do it like this to work. You need to cast to ValueTuple<double, double> and not (double, double).

var geo = CacheEngine.Get(key);
if (geo != null)
{
    var geoTuple = (ValueTuple<double, double>)geo;
    if (!geoTuple.Equals(default(ValueTuple<double, double>)))
    return geoTuple;
}
like image 181
Chris Auer Avatar answered Jan 12 '23 02:01

Chris Auer