{"id":8756,"date":"2023-04-19T14:09:03","date_gmt":"2023-04-19T11:09:03","guid":{"rendered":"https:\/\/eran.geek.co.il\/wp\/?p=8756"},"modified":"2023-04-30T21:20:20","modified_gmt":"2023-04-30T18:20:20","slug":"unity-tidbit-null-coalescing-null-propagation","status":"publish","type":"post","link":"https:\/\/eran.geek.co.il\/wp\/archives\/8756","title":{"rendered":"<strong>Unity Tidbit: Null Coalescing Null Propagation With Nullable Structs<\/strong>"},"content":{"rendered":"\n<p>You know Null Propagation, right? If you need a value but you&#8217;re not sure the object exists and is initialised so you add a little ? before the value call you want? You know, if you have an array and you need array[0] but you&#8217;re not sure array is not null, you just write&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array?&#91;0]<\/code><\/pre>\n\n\n\n<p>This way, the call doesn&#8217;t throw and, worst case, you get a null.<\/p>\n\n\n\n<p>But what if it&#8217;s supposed to be an array of primitives like integers or anything not nullable? You can&#8217;t just do&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int number = array?&#91;0];<\/code><\/pre>\n\n\n\n<p>The compiler gets angry at you for trying to assign a Nullable&lt;int&gt; into an int. That&#8217;s a big no no.<\/p>\n\n\n\n<p>Well, what doesn&#8217;t work with Null Propagation works with Null Coalescing!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int number = array?&#91;0] ?? 0;<\/code><\/pre>\n\n\n\n<p>This way, the first ? checks if the array is null. If it&#8217;s not, you get the first value. If it is, you get a null and the next ?? checks if the left side is null. If it is, it gives you 0 instead. Now you&#8217;re assigning ints to ints and everything is cool.<\/p>\n\n\n\n<p>This also works great when you want optional parameters but your parameters don&#8217;t exactly have well defined default values or you want to use default values for your parameters that are not compile constants.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void Method(Vector3? positionOffset = default)\n{\n    Vector3 localPositionOffset = positionOffset ?? Vector3.zero;\n    \/\/ more logic...\n}<\/code><\/pre>\n\n\n\n<p>Because Nullable types&#8217; default value is null.<\/p>\n\n\n\n<p>You&#8217;re welcome. \ud83d\udd96<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to check for null and still get the primitive you want, in one line of code.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[157,158,144,153],"class_list":["post-8756","post","type-post","status-publish","format-standard","hentry","category-_","tag-c","tag-programming","tag-unity","tag-work"],"_links":{"self":[{"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/posts\/8756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/comments?post=8756"}],"version-history":[{"count":3,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/posts\/8756\/revisions"}],"predecessor-version":[{"id":8767,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/posts\/8756\/revisions\/8767"}],"wp:attachment":[{"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/media?parent=8756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/categories?post=8756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eran.geek.co.il\/wp\/wp-json\/wp\/v2\/tags?post=8756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}