如果不为null则赋值属性,否则不执行任何操作

时间:2017-08-17 09:37:45

标签: c# properties nullable

我想压缩此代码:

 $scope.items = itemsResponse.data.main_themes

如果if(obj != null) { obj.Name = "Test"; } 有明确的方法来设置我可以写的obj

Name

没有明确的方法声明是否可以这样做?

类似的东西:

obj?.SetName("Test");

最后一个语句给出了以下错误obj?.Name = "Test"; //assign obj.Name to "Test" if obj not null, do nothing if obj is null 哪个有意义,因为The left-hand side of an assignment must be a variable, property or indexer现在是一个可以为空的布尔值我相信。

编辑: 这个问题似乎重复了;

Using the null-conditional operator on the left-hand side of an assignment

0 个答案:

没有答案