Why can one null return be tested but another throws an exception?

Posted by mickey on Stack Overflow See other posts from Stack Overflow or by mickey
Published on 2010-05-18T14:50:49Z Indexed on 2010/05/18 15:00 UTC
Read the original article Hit count: 218

Filed under:
|
|
|
|

I want to test if an xml attribute is present. Given this:

XmlAttributeCollection PG_attrColl = SomeNodeorAnother.Attributes;

This first test works:

if (null != PG_attrColl["SomeAttribute"])   

"GetNamedItem" is supposed to return null, but the following test throws an exception complaining about the null it returns.

if (null != PG_attrColl.GetNamedItem("SomeAttribute").Value;)

Why the difference? Just curious.

© Stack Overflow or respective owner

Related posts about Xml

Related posts about attribute