只需执行null检查+属性访问即可

时间:2019-02-19 13:14:45

标签: c# properties null call

是否有运算符可以简化C#中的此操作以避免空指针异常?

obj == null ? null : obj.Property;

类似

obj?.Property;

我真的想摆脱NullReferenceExeptions

1 个答案:

答案 0 :(得分:1)

正如@ canton7所说,您回答了自己的问题。 library(dplyr) setregion <- function(uf) { region_out <- case_when( uf %in% c("RO","AC","AM","RR","PA","AP","TO") ~ "North", uf %in% c("MA","PI","CE","RN","PB","PE","AL","SE","BA") ~ "Northeast", uf %in% c("MG","ES","RJ","SP") ~ "Southeast", uf %in% c("PR", "SC", "RS") ~ "South", uf %in% c("MS","MT","GO", "DF") ~ "Midwest" ) return(region_out) } 运算符实际上在C#中存在

下面是一个小示例,展示了它如何防止?.

NullReferenceException