C# 无论c中的条件如何,字符串的值都设置为null#
我正在给c中的var赋值# 现在我面临的问题是,不管C# 无论c中的条件如何,字符串的值都设置为null#,c#,C#,我正在给c中的var赋值# 现在我面临的问题是,不管properties.AfterProperties[“Name”] 我检查了带有文件名的properties.AfterProperties[“Name”]的值,还检查了即时窗口中的整个赋值语句,并将properties.AfterProperties[“Name”]的值赋值给文件名 但当我在该赋值行后按f11时,fileName的值为空 这取决于后置属性的类型 在第一个条件中,您使用了properties.AfterProperties[“
properties.AfterProperties[“Name”]
我检查了带有文件名的properties.AfterProperties[“Name”]
的值,还检查了即时窗口中的整个赋值语句,并将properties.AfterProperties[“Name”]
的值赋值给文件名
但当我在该赋值行后按f11时,
fileName
的值为空 这取决于后置属性的类型
在第一个条件中,您使用了properties.AfterProperties[“Name”]
,但在分配中,您使用了properties.AfterProperties[“Name”].ToString()
可能properties.AfterProperties[“Name”]
不为null,但properties.AfterProperties[“Name”].ToString()返回null。可能会这样尝试:
字符串文件名=!properties.AfterProperties[“Name”]。是否等于(null)?properties.AfterProperties[“Name”].toString():“”代码>您也可以试试这个
string fileName = properties.AfterProperties["Name"] != null && !String.IsNullOrEmpty(properties.AfterProperties["Name"].toString()) ? properties.AfterProperties["Name"].toString(): "";
顺便提醒一下:
properties.AfterProperties["Name"] != null // This check avoids object reference errors.
!String.IsNullOrEmpty(properties.AfterProperties["Name"].toString()) // This check will avoid your problem - returning empty instead of null.
希望这能有所帮助。我还尝试删除了.toString():(但没有成功)后属性的类型是什么?
properties.AfterProperties["Name"] != null // This check avoids object reference errors.
!String.IsNullOrEmpty(properties.AfterProperties["Name"].toString()) // This check will avoid your problem - returning empty instead of null.