简体   繁体   中英

C# check null on an object and its property

I'd like to tidy up this piece of code, any idea?

  • 1 object is not null

  • 1 of its property is not null either

     var v = Values.Find(x => x.id.Equals(Properties.Resources.myString)); if (v != null && v.Property != null) { // do something with 'v.Property' } 

C# 6.0 will probably best serve you here with the null conditional operator:

var v = Values.Find(x => x.id.Equals(Properties.Resources.myString));
var result = v?.Property?.DoSomething();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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