Let cls
be of type XmlNode
Following statement allows me to access child nodes:
foreach (XmlNode child in cls.ChildNodes)
Now when I try to use var:
foreach (var child in cls.ChildNodes)
then the type of child is not XmlNode
, only object. I cannot use child.NodeType
, compiler says:
object' does not contain a definition for 'NodeType
Why is this?
ChildNodes
is of type XmlNodeList
which is defined as
public abstract class XmlNodeList : IEnumerable, IDisposable
Its enumerator returns an object
. When you use XmlNode
instead of var
, .net automatically casts
object to XmlNode
. But when you use var
, child is treated as an object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With