Is there an “opposite” to the null coalescing operator? (…in any language?)

There’s the null-safe dereferencing operator (?.) in Groovy… I think that’s what you’re after. (It’s also called the safe navigation operator.) For example: homePostcode = person?.homeAddress?.postcode This will give null if person, person.homeAddress or person.homeAddress.postcode is null. (This is now available in C# 6.0 but not in earlier versions)

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

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

Is there a VB.NET equivalent for C#’s ‘??’ operator?

Use the If() operator with two arguments (Microsoft documentation): ‘ Variable first is a nullable type. Dim first? As Integer = 3 Dim second As Integer = 6 ‘ Variable first <> Nothing, so its value, 3, is returned. Console.WriteLine(If(first, second)) second = Nothing ‘ Variable first <> Nothing, so the value of first is … Read more

?? Coalesce for empty string?

C# already lets us substitute values for null with ??. So all we need is an extension that converts an empty string to null, and then we use it like this: s.SiteNumber.NullIfEmpty() ?? “No Number”; Extension class: public static class StringExtensions { public static string NullIfEmpty(this string s) { return string.IsNullOrEmpty(s) ? null : s; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)