I would like to change how default(T) behaves for certain classes. So instead of returning null for my reference types I would like to return a null object.
Kind of like
kids.Clear();
var kid = kids.Where(k => k.Age < 10).SingleOrDefault();
if (kid is NullKid)
{
Console.Out.WriteLine("Jippeie");
}
Anyone know if this is at all possible?
I don't think, it is possible. What you could do however, is to create your own extension method
SingleOrCustomDefault
or something like that.How about this:
It is simply not possible at all.
But maybe you want to use DefaultIfEmpty instead:
You can't change default(T) - it's always null for reference types and zero for value types.
I think you've already got the answer in your question: if/switch statement. Something like this:
You can make your own extension method like this:
Usage: