繁体   English   中英

{}在C#中是什么意思?

[英]What does {} mean in C#'

我有一个类型为System.Single的DataGridView单元。

if (myDataGridView.Rows[0].Cells[1].Value == null) 
{
    //some code
}

myDataGridView.Rows[0].Cells[1].Value值为{} 它不是null ,也不是0==的右边应该使条件为真?

在使用它之前,它被称为DBNull.Value

if (myDataGridView.Rows[0].Cells[1].Value == DBNull.Value) 
{
    //some code
}

如前所述,该值是DbNull的实例。

鉴于DbNull.Value是一个单例(因此可以安全地进行参考比较),想到了两个选择:

if (myDataGridView.Rows[0].Cells[1].Value == DBNull.Value) 

要么

if (myDataGridView.Rows[0].Cells[1].Value is DbNull) 

我个人很喜欢后者-它适合数据库中的“ is null”方法,而且读起来也很不错……这清楚表明我对null作为一种属性感兴趣,而不是执行实际的相等性比较。

你有尝试过吗?

if (DBNull.Value.Equals(myDataGridView.Rows[0].Cells[1].Value)) 
{
    //some code
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM