Why does FirstNode.GetType() return an XElement and not a XNode

Viewed 61

The c# docs says that the FirstNode property returns a XNode.

public System.Xml.Linq.XNode FirstNode { get; }

However when i call the GetType() method on it, it says System.Xml.Linq.XElement

For example..

var MyElement = new XElement("Base",
                new XElement("FirstName", "John",
                    new XComment("Thats a cool name")),
                new XElement("LastName", "Doe")
            );

Console.WriteLine(MyElement.FirstNode.GetType());//System.Xml.Linq.XElement

Further more, It doesnt have the methods which apply to XElements like the property FirstNode itself. Can someone explain what is going on.

2 Answers

Object.GetType returns the exact runtime type of the current instance(docs). And node returned by First in your case is XElement, which is descendant of XNode.

Type hierarchy for XElement from docs:

Object -> XObject -> XNode -> XContainer -> XElement

According to the docs, XElement inherits from XNode. FirstNode can be anything that inherits from XNode.

Related