For reasons I won't bore you with, I have a generic object whose value is null
, and I need to convert it to a nullable int.
object foo = null
int? bar = Convert.ToInt32(foo) // bar = 0
int? bar = (int?)Convert.ToInt32(foo) // bar = 0
int? bar = Convert.ToInt32?(foo) // not a thing
From this thread:
int? bar = Expression.Constant(foo, typeof(int?)); // Can not convert System.Linq.Expression.Constant to int?
I need bar
to be null
. How do I do this?