简体   繁体   English

如果C#中为空对象,则返回null

[英]Return null in case of empty object in C#

I try this statement to check if SSN is not empty return the value in SSNstr otherwise return null. 我尝试使用此语句检查SSN是否不为空,返回SSNstr中的值,否则返回null。 But this is returning me error Object reference not set to an instance of an object. 但这返回了错误Error Object reference not set to an instance of an object. when SSN is empty. 当SSN为空时。

  Model.DocumentData dt = new Model.DocumentData();     
 dt.SSNstr = (dt.SSNstr == null ? null :(string)individual.XPathSelectElement("Individual/SSN")).Insert(5,"-").Insert(3,"-");

this is dt class: 这是dt类:

      [DataMember]
      public string SSNstr = string.Empty;

Please attention here I know how to check null value but I like to do it in same line of code. 请注意这里,我知道如何检查空值,但是我喜欢在同一行代码中进行检查。 As I have so many fields like this that I need to check I prefer to check in 1 line of the code or change something in dt class that take care of it. 因为我有很多这样的字段需要检查,所以我宁愿签入代码的第一行或更改dt类中的代码来处理它。

Null and "empty string" are two different things. Null和“空字符串”是两个不同的东西。 Make your check with String.IsNullOrEmpty or String.IsNullOrWhiteSpace . 使用String.IsNullOrEmptyString.IsNullOrWhiteSpace检查。

At the end of that, the result of your XPath query might be empty. 最后,您的XPath查询结果可能为空。 Why don't you break that up into a few lines? 你为什么不把它分成几行?

Addendum: It really looks like your XPath query result is empty. 附录:确实看起来您的XPath查询结果为空。 You're working on the assumption that it will not be empty. 您正在假设它不会为空。 Do you have that assurance? 你有那个保证吗? When you're programming, don't trust anyone. 当您编程时,不要信任任何人。 Including yourself. 包括你自己。

Check your parentheses. 检查您的括号。

If dt.SSNstr is null , the expression is not returning null , it is returning (null).Insert(5,"-").Insert(3,"-") which causes the error. 如果dt.SSNstrnull ,则表达式不返回null ,而是返回(null).Insert(5,"-").Insert(3,"-") ,这将导致错误。 You can't insert anything into null . 您不能在null插入任何内容。

One solution would be to write 一种解决方案是写

dt.SSNstr = dt.SSNstr == null ? null :(string)individual.XPathSelectElement("Individual/SSN").Insert(5,"-").Insert(3,"-");

but then if would be better to have 但是如果有的话会更好

if (dt.SSNstr!=null)
  dt.SSNstr =(string)individual.XPathSelectElement("Individual/SSN").Insert(5,"-").Insert(3,"-");

otherwise you're assigning null to a variable that is already null. 否则,您要将null分配给已经为null的变量。

Edit: As the other answers say, you should check for IsNullOrEmpty , not just equal to null . 编辑:正如其他答案所说,您应该检查IsNullOrEmpty ,而不仅仅是等于null You start out with string.Empty , but later you assign null to it. 您从string.Empty开始,但后来为它分配了null

dt might be null in this case. 在这种情况下, dt可能为null。 I would check if that object actually has a value. 我将检查该对象是否实际具有值。 You would need to instantiate dt , then you can set the value of dt.SSNstr . 您将需要实例化dt ,然后可以设置dt.SSNstr的值。

根据您的修改,您可以像

    dt.SSNstr = ((individual.XPathSelectElement("Individual/SSN") as string) == null) ? null : (individual.XPathSelectElement("Individual/SSN") as string).Insert(5,"-").Insert(3,"-");

var test = ""; var test =“”;

var a = string.IsNullOrEmpty(test) ? var a = string.IsNullOrEmpty(test)吗? null : test; null:测试;

I hope it´s Helps. 希望对您有所帮助。

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

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