-3

I am well aware of ternaries. Just wondering what on earth the question mark next to "l" in the following line of code:

public static string GetRegion()
        {
            var ipAddress = GetIPAddress();
            if (string.IsNullOrEmpty(ipAddress))
            {
                return "";
            }

            try
            {
                var ls = new LookupService(HttpContext.Current.Server.MapPath("~") + "/GeoIP/GeoIPCity.dat", LookupService.GEOIP_STANDARD);
                var l = ls.getLocation(ipAddress);

                return l?.region ?? "";
            }
            catch (Exception)
            {
                return "";
            }
        } 

what does l?.region ?? "" mean?

d1du
  • 296
  • 1
  • 3
  • 12
  • see [this](https://msdn.microsoft.com/en-gb/magazine/dn802602.aspx) and [this](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator) – Ousmane D. Dec 10 '18 at 18:58
  • so how exactly am I supposed to word this question when I don't know how to ask? Yes, I know that this question probably has been asked before. No duh. I just didn't know what to google search that would lead me to it as the search I did to describe this situation lead me to many many results (and no, googling null propagation or null coalesce isn't obvious to someone who is unfamiliar with the syntax). If I knew how to ask the question, I wouldn't even need stack overflow. seriously, you mods have deep rooted issues. – d1du Mar 27 '19 at 19:50

1 Answers1

1

That's a mix of Null Propagation and Null Coalesce operator and thus l?.region ?? "" region will be evaluated only iff l is not null and if l.region evaluates to null then return a default value which is empty string

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • thank you for your explanation. The key words really helped in googling the right documentation – d1du Mar 27 '19 at 19:55