In this post, we will see how to resolve Initialize a value when not null (or leave the default if new value is null)
Question:
If I have an object, that has defaults defined so they are not nullSomeList
only if myListThatMightBeNull
isn’t null, and leave the default value otherwise?Doing this obviously works, it just feels clunky compared to other modern syntax.
=??
operator?Best Answer:
You can use the null coalescing??
operator while assigning this list.
On the Microsoft website, you can find its documentation hereso you code will be as following:
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com
Leave a Review