I have list that that either will be null or has a single list value.
I want to be able to convert this list to return default value according to the calling function as per below. Can someone help in figuring out how to define GetValue()?
var xx = new List<int>{1};
var yy = new List<int?>{};
var zz = new List<int?>{1};
Getvalue<int>(xx);//return 1
Getvalue<long>(xx);//return 1
Getvalue<int?>(xx);//return 1
Getvalue<long?>(xx);//return 1
Getvalue<int>(yy);//return 0
Getvalue<long>(yy);//return 0
Getvalue<int?>(yy);//return null
Getvalue<long?>(yy);//return null
Getvalue<int>(zz);//return 1
Getvalue<long>(zz);//return 1
Getvalue<int?>(zz);//return 1
Getvalue<long?>(zz);//return 1
I have below function, but it only work for normal type like integer, long, but I need something that work with List instead now...
public T GetValue<T>(object value)
{
if (value == null || value == DBNull.Value)
{
return default(T);
}
else
{
//return (T)value;
return (T)Convert.ChangeType(value, typeof(T));
}
}
Most efficient way to check for DBNull and then assign to a variable?