Unique ways to use the null coalescing operator [closed]

Well, first of all, it’s much easier to chain than the standard ternary operator: string anybody = parm1 ?? localDefault ?? globalDefault; vs. string anyboby = (parm1 != null) ? parm1 : ((localDefault != null) ? localDefault : globalDefault); It also works well if a null-possible object isn’t a variable: string anybody = Parameters[“Name”] ?? … Read more

C#’s null coalescing operator (??) in PHP

PHP 7 adds the null coalescing operator: // Fetches the value of $_GET[‘user’] and returns ‘nobody’ // if it does not exist. $username = $_GET[‘user’] ?? ‘nobody’; // This is equivalent to: $username = isset($_GET[‘user’]) ? $_GET[‘user’] : ‘nobody’; You could also look at short way of writing PHP’s ternary operator ?: (PHP >=5.3 only) … Read more

How am I misusing the null-coalescing operator? Is this evaluating “null” correctly?

This is Unity screwing with you. If you do this: MeshFilter GetMeshFilter() { MeshFilter temp = myGo.GetComponent<MeshFilter>(); if ( temp == null ) { Debug.Log( ” > Get Mesh Filter RETURNING NULL” ); return null; } return temp; } It works. Why? Because Unity has overridden Equals (and the == operator) on all Unity objects … Read more

An expression tree lambda may not contain a null propagating operator

The example you were quoting from uses LINQ to Objects, where the implicit lambda expressions in the query are converted into delegates… whereas you’re using EF or similar, with IQueryable<T> queryies, where the lambda expressions are converted into expression trees. Expression trees don’t support the null conditional operator (or tuples). Just do it the old … Read more

PHP short-ternary (“Elvis”) operator vs null coalescing operator

Elvis ?: returns the first argument if it contains a “true-ish” value (see which values are considered loosely equal to true in the first line of the Loose comparisons with == table). Or the second argument otherwise $result = $var ?: ‘default’; // is a shorthand for $result = $var ? $var : ‘default’; Null … Read more