c# - 有没有一种方法可以在检查变量是否为空的同时为变量赋值?
问题描述
我目前有这个代码:
private static void IsEntryWidthChanged(BindableObject bindable, object oldValue, object newValue)
{
if ((bindable as SingleEntryGrid)._column3 != null)
(bindable as SingleEntryGrid)._column3.Width = (double)newValue;
}
这是一种我可以将(可绑定为 SingleEntryGrid)分配给变量的方法,同时检查它是否为空,然后将其用于下一行,以便下一行变为:
singleEntryGrid._column3.Width = (double)newValue;
解决方案
如果现有对象为空,我想您可以使用它??=
来提供新对象:
((bindable as SingleEntryGrid)._column3 ??= new ColumnThing()).Width = newValue;
替换ColumnThing
为具有的类型_column3
。{ X = Y }
您可以在初始化器之后设置更多属性()
推荐阅读
- python - 当该字段存在映射时,Elasticsearch“无法找到 geo_point 字段 [位置]”
- android - Firebase Cloud 消息不适用于 Android 上的 Build 变体
- javascript - Node JS driver.findElements(by.className('classname').getText() 抛出错误
- sql - 此 SQL 语句的性能问题
- java - 使用 System.load 从指定路径加载 .so 库
- webrtc - WebRTC 图片捕获不适用于智能手机
- c# - 单元测试仅在第二次运行时有效
- java - 如何在 Java Swing 中获取 JTextField 的值?
- python - 通过python禁用谷歌驱动器文件的下载选项
- php - 根据 Woocommerce 结帐中的产品类别更改“电子邮件”标签