Your question got me curious. I was wondering why null was appropriate here. Turns out this got changed in V11, it will now guarantee an empty enumerable instead of null.
Good to know is that just to be safe I would still do the null check in V10 even though they can get a bit annoying.
Good to know that in V11 the empty enumerable is guaranteed and in my opinion this is the right choice instead of returning null.
The reason why the method can return null is not clear to me, but so far I have never run into a case where it returns null, this is the only thing I can report.
In addition to that, I can say that there are also other methods/properties which behave in the same way such as the Multiple Image Picker: it always returns an empty list even if no image has been selected.
Does it make sense for this properties to check for null even though I'm sure it will never be returned? Isn't it better to use the null-forgiving operator (!) to make the code cleaner and avoid unnecessary checks?
Children<T>() never returns null
Hi everyone!
I was wondering why this piece of code:
doesn't returns null when there are no children. It returns always an empty collection so why Children() has nullable returning type?
Can I safely use this code:
Thanks!
Hi,
What version of Umbraco are you using? I am going with Umbraco 10 for now.
Looking at the source code from V10 this can return null. The method is marked as
IEnumerable<IPublishedContent>?
.You should check for
null
, a easy way of doing it is like this:Your question got me curious. I was wondering why null was appropriate here. Turns out this got changed in V11, it will now guarantee an empty enumerable instead of null.
Good to know is that just to be safe I would still do the null check in V10 even though they can get a bit annoying.
Hi Rob,
I'm using Umbraco version 10.4.0.
Good to know that in V11 the empty enumerable is guaranteed and in my opinion this is the right choice instead of returning null.
The reason why the method can return null is not clear to me, but so far I have never run into a case where it returns null, this is the only thing I can report.
In addition to that, I can say that there are also other methods/properties which behave in the same way such as the Multiple Image Picker: it always returns an empty list even if no image has been selected.
Does it make sense for this properties to check for null even though I'm sure it will never be returned? Isn't it better to use the null-forgiving operator (!) to make the code cleaner and avoid unnecessary checks?
is working on a reply...