newContent.CreateUserID = sheet.rows[i][0] == null ? "" : sheet.rows[i][0] as String;,这段Dart代码,还有更简单的写法吗?
时间: 2023-06-26 11:03:45 浏览: 45
可以使用空值运算符 `??` 和 null-aware 选择运算符 `?.` 来简化代码,如下所示:
```
newContent.CreateUserID = sheet.rows[i][0] ?? "" as String;
```
其中,空值运算符 `??` 表示如果 `sheet.rows[i][0]` 是 null,则使用空字符串 `""`;null-aware 选择运算符 `?.` 则表示只有在 `sheet.rows[i][0]` 不为 null 时才调用 `as String`。
相关问题
newContent.CreateUserID = sheet.rows[i][0] == null ? "" : sheet.rows[i][0] as String;,还有更简单的写法吗?
### 回答1:
可以使用 C# 6.0 引入的 null 条件运算符,简化代码如下:
```
newContent.CreateUserID = sheet.rows[i][0] as string ?? "";
```
或者使用 C# 7.0 引入的空合并运算符,代码如下:
```
newContent.CreateUserID = sheet.rows[i][0] as string ?? "";
```
以上两种写法都会先将 `sheet.rows[i][0]` 强制转换为 `string` 类型,如果转换失败则返回 `null`。然后使用 null 合并运算符或空合并运算符,如果结果为 `null` 则返回空字符串。
### 回答2:
对于给定的代码 `newContent.CreateUserID = sheet.rows[i][0] == null ? "" : sheet.rows[i][0] as String;`,如果希望将其更简化,可以使用 Java 8 引入的 Optional 类来处理可能为 null 的值。可能的简化代码如下:
```java
newContent.CreateUserID = Optional.ofNullable(sheet.rows[i][0]).map(Object::toString).orElse("");
```
上述代码中,`Optional.ofNullable(sheet.rows[i][0])` 将 `sheet.rows[i][0]` 转化为 Optional 对象,然后使用 `map` 方法将其映射为字符串,最后使用 `orElse` 方法在 `sheet.rows[i][0]` 为 null 时提供一个默认值。
使用上述简化代码可以避免显式的 null 检查以及类型转换,提高代码的可读性和简洁性。
### 回答3:
可以使用条件运算符(三元运算符)简化这个表达式。表达式的含义是如果`sheet.rows[i][0]`为空,则`newContent.CreateUserID`等于空字符串,否则将`sheet.rows[i][0]`转换为字符串赋值给`newContent.CreateUserID`。
利用条件运算符可以将这个表达式简化为:
`newContent.CreateUserID = sheet.rows[i][0]?.ToString() ?? "";`
这里使用了空值合并运算符(`??`),如果`sheet.rows[i][0]?.ToString()`为空,则返回空字符串;否则将`sheet.rows[i][0]?.ToString()`的结果赋值给`newContent.CreateUserID`。
使用简化后的写法,可以将条件判断和类型转换合并在一个表达式中,使代码更加简洁。
dataInfo = new DeviceManage.设备运行(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Selected == true) { dataInfo.dev_no = sbbh.Text = dataGridView1.Rows[i].Cells[0].Value.ToString(); dataInfo.date = dateTimePicker1.Text = dataGridView1.Rows[i].Cells[1].Value.ToString(); dataInfo.starttime = dateTimePicker2.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.endtime = dateTimePicker3.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.endwhy = tjyy.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.devstatus = yxzt.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); dataInfo.remark = bz.Text = dataGridView1.Rows[i].Cells[6].Value.ToString(); } 同义改写
以下是对上述代码的同义改写:
```
DeviceManage.设备运行 dataInfo = new DeviceManage.设备运行();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Selected == true)
{
sbbh.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
dateTimePicker1.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
dateTimePicker2.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
dateTimePicker3.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
tjyy.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
yxzt.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
bz.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
dataInfo.dev_no = sbbh.Text;
dataInfo.date = dateTimePicker1.Text;
dataInfo.starttime = dateTimePicker2.Text;
dataInfo.endtime = dateTimePicker3.Text;
dataInfo.endwhy = tjyy.Text;
dataInfo.devstatus = yxzt.Text;
dataInfo.remark = bz.Text;
}
}
```
这个代码段的作用是将选中的行中的数据填充到表单上,并将该行数据保存在 `dataInfo` 对象中。同义改写后的代码与原代码的功能相同,只是稍微重构了一下。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)