问号后跟一个句点如果紧挨着一个变量是什么意思?
What does the question mark followed by a period mean if it is next to a variable?
我很了解三元组。只是想知道以下代码行中 "l" 旁边的问号到底是什么:
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 "";
}
}
l?.region 是什么?? ""是什么意思?
这是 Null Propagation 和 Null Coalesce 运算符的混合,因此 l?.region ?? ""
region
将被评估只有当 l
不为 null 并且如果 l.region
计算为 null
则 return 为空字符串的默认值
我很了解三元组。只是想知道以下代码行中 "l" 旁边的问号到底是什么:
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 "";
}
}
l?.region 是什么?? ""是什么意思?
这是 Null Propagation 和 Null Coalesce 运算符的混合,因此 l?.region ?? ""
region
将被评估只有当 l
不为 null 并且如果 l.region
计算为 null
则 return 为空字符串的默认值