I have a number of nullable objects of different types (e.g. DateTime?, Guid?) that I want to check for a value.
I'd like to avoid code like this:
return SomeGuid.HasValue || SomeBool.HasValue || SomeDateTime.HasValue
but it does not seem possible to create an array of Nullable<object>. I was hoping to do something like:
return new Nullable<object>[] { SomeGuid, SomeBool, SomeDateTime }.Any(o => o.HasValue);
object is nullable per definition.
So the following works:
Guid? guid = null;
bool? boolean = null;
DateTime? date = DateTime.Now;
var test = new object[] { guid, boolean, date }.Any(o => o != null);
Do note Nullable<T> requires T to be a struct, i.e. a value type (not a reference type). object is a reference type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With